我正在尝试执行以下操作:
一种计算猫 ArrayList 中所有猫腿总数的方法。
但是我似乎无法让它添加数组中猫腿的总数,或将其显示在控制台上。
public static int getNumOfLegs()
{
return numOfLegs;
}
public void setNumOfLegs(int numOfLegs)
{
this.numOfLegs = numOfLegs;
}
public static int totalNumOfLegs(ArrayList<Cat> catList)
{
int total = 0;
for (Cat c: catList)
{
total = total + getNumOfLegs();
}
return total;
}
非常感谢。
最佳答案
更改:
total = total + getNumOfLegs();
致:
total = total + c.getNumOfLegs();
关于java - 对数组列表中的元素进行总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18939747/