java - 对数组列表中的元素进行总计

标签 java

我正在尝试执行以下操作:

一种计算猫 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/

相关文章:

java - Mapbox Android SDK - 致命信号 11 (SIGSEGV)

java - 如何在 recyclerview 中添加聊天日期?

javascript - AJAX 调用未将数据发送到 Controller 方法

java - 是否可以让空的 RequestParam 值使用 defaultValue?

java - Java/Scala 中的简单视频解码

通过 Internet 保存 Java 属性

java - 为什么 Hibernate 尝试插入具有已存在 id 的新记录?

java - 最佳跨平台 GUI 和 USB 语言

java - 为不带参数的 boolean 方法编写 Junit 测试

java - 安卓奇巧 : fileChooser + fileTransfer cordova plugin not working