我正在研究表示整数列表的NumberList
类。 NumberList
对象只有一个实例变量values
,它是对int
值数组的引用。我需要实现的方法之一应该返回列表中所有条目的总和。
这是我的尝试:
public long getTotal()
{
long total = 0;
for (int i = 0; i < total; i++)
{
total += values[i];
}
return total;
}
最佳答案
您的for循环上升到total
,但这(可能)不是数组的大小,而是values.length
。
关于java - 如何返回列表中所有条目的总和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5574472/