所以我有一个 arrayList
Dog spot = new Dog("Spot", "Lab","Blonde", Tail.OTTER,"Yes", 50, 3);
Dog buddy = new Dog("Buddy", "Pit Bull", "Black", Tail.DOCKED,"Yes", 60, 2);
Dog mia = new Dog ("Mia", "Pug", "Brown", Tail.RING ,"No", 70, 4);
现在 50、60 和 70 是狗的体重。我想将它们加起来,而不需要编写冗长的、不灵活的代码,例如:
int totalWeight = spot.weight + buddy.weight + mia.weight;
有办法做到这一点吗?
最佳答案
int totalWeight = 0;
for (final Dog dog : dogs) {
totalWeight += dog.getWeight();
}
关于java - 如何计算 arrayList 中的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18938699/