获取java方法中对象的基本信息

获取java方法中对象的基本信息,java,arraylist,Java,Arraylist,因此,在一个单独的程序中,我声明了一个名为dragon的对象,如下所示: public Dragon(int color, int location, boolean breathesFire) { this.color = color; this.location = location; this.breathesFire = breathesFire; } 现在我想制作一个方法,在龙的数组列表中对一条特定的龙进行操作,以改变它的一个基本元素,比如颜色 于是我写道

因此,在一个单独的程序中,我声明了一个名为dragon的对象,如下所示:

public Dragon(int color, int location, boolean breathesFire) {
    this.color = color;
    this.location = location;
    this.breathesFire = breathesFire;
  }
现在我想制作一个方法,在龙的数组列表中对一条特定的龙进行操作,以改变它的一个基本元素,比如颜色

于是我写道:

public void changeColor(int i) {
    int j = (int) Math.random() * 7; 
    int k = this.location;
    boolean o = this.breathesFire;
    army.set(i, j, k, o);
  }

假设更改数组列表中第i条龙的颜色(它们是6种颜色,因此Math.random*7向下舍入将得到一种随机颜色)。不管怎样,它不会编译并且有各种错误,有什么提示/解决方案吗?

您不应该在Dragon类中声明这样的方法。在包含ArrayList对象的类中声明它。 你要做的第一件事是从阵法列表中接收第i条龙。 然后,您应该为Dragon的成员使用getter

    int j = (int) Math.random() * 7; 
    Dragon dragon = yourArrayList.get(i);
    int k = dragon.getLocation();
    boolean o = dragon.isBreathingFire();
    army.set(i, j, k, o);
getter(在Dragon类中声明)将是


这就是我所能想到的。要获得更多帮助,我们需要完整的错误消息。

如果您的代码未编译,并且需要帮助,请显示完整的错误消息。粘贴所有必需的代码。陆军指的是什么对象,set方法逻辑做什么?你必须发布你得到的错误。你的问题不清楚。试着重新表述你的问题。
    public boolean isBreathingFire() {
        return breathesFire;
    }

   public int getLocation() {
        return location;
    }