I am working on this NumberList class which represents a list of integers. The NumberList object has just one instance variable values, which is a reference to an array of int values. One of the methods I need to implement is supposed to return the sum of all entries in the list.
Here is my attempt at it:
public long getTotal()
{
long total = 0;
for (int i = 0; i < total; i++)
{
total += values[i];
}
return total;
}
Your for loop is going up to
total, but that’s (probably) not the size of your array, it’svalues.length.