我目前有 2 个类(class)。我的机器人:
public class Bot {
private int X;
private int Y;
private int degress;
public int getX() {
return X;
}
public int getY() {
return Y;
}
public int getDegress() {
return degress;
}
public void setX(int X) {
this.X = X;
}
public void setY(int Y) {
this.Y = Y;
}
}
和我的主要代码.. 我遇到的问题是在我的主课上设置学位时。 我想做的是类似于:
bots.set(bots.get(1).getDegress(),+1);
但给我这个错误“ArrayList 类型中的方法 set(int, Bot) 不适用于参数 (int, int)”
以及我的 ArrayList 的样子
public static ArrayList<Bot> bots = new ArrayList<Bot>();
总结一下。我怎么能用 bots.set 来改变 X、Y 或 Degress?
最佳答案
bots.set
是一个 ArrayList
方法。要改变 Bot
实例,您必须调用 Bot
方法。
应该是:
Bot bot = bots.get(1);
bot.setDegrees(bot.getDegrees()+1);
当然,您的 Bot
类中需要一个 setDegrees
方法。
关于java - ArrayList:当你有多个 setter 时如何设置 arraylist.set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53816683/