Java Reference
In-Depth Information
for (int i = 1; i < size; i++) {
maxValue = Math.max(maxValue, elementData[i]);
}
return maxValue;
}
9. The preconditions are that the client will not try to construct a list with a negative
capacity and that the client will never pass an index that is negative or outside the
size of the list.
10. The checkIndex method tests whether a given index is between 0 and the size of
the list and, if it is not, throws an exception. If the client passes an invalid index by
mistake, the method will halt the program's execution.
11. The checkCapacity method tests whether the array's size will exceed the length
of the internal array (capacity) and, if it will, throws an exception. If the client adds
too many elements to the list, the method will halt the program's execution.
12. With our preconditions we may now assume that size <= capacity at all times.
We can also assume that all index parameters passed to various methods are valid
once they get through the checkIndex test.
13. These methods are provided as a convenience to the client to give the list object a
more simple and pleasant external interface to use.
14. The list doubles in size when it exceeds its capacity. We do this instead of resizing
by a constant amount so that the overall cost of adding elements to the end of a list
will be amortized to be O(1), constant time.
15. An iterator provides a standard way of examining the elements of a collection.
16. The iterator keeps track of the list to examine, the current index in the list, and
whether it is safe to remove an element from the list using the iterator.
17. The iterator knows that there are more elements to examine if its current index is
below the size of the list. If there is no such element but the client calls next ,an
exception is thrown.
18. The precondition of remove is that the method next has been called and that next
was called more recently than any other call to remove . The precondition is
enforced by a boolean flag in the iterator whose value is changed on every call to
next or remove . If the precondition is violated, an exception is thrown.
19. public int sum() {
int total = 0;
for (int i = 0; i < size; i++) {
total += elementData[i];
}
return total;
}
Search WWH ::




Custom Search