Java Reference
In-Depth Information
Here is the new version of the code:
ArrayList.this.remove(position - 1);
Before we finish this exploration, we should consider at least briefly the issue of
interfaces. As we saw in Chapters 9 and 11, interfaces allow clients to describe vari-
ables in a more generic way. For example, suppose that you want to iterate over an
ArrayList
of
String
values. You'll replace the following line of code:
ArrayListIterator i = list.iterator();
Instead, you'll use the
Iterator<E>
interface:
Iterator<String> i = list.iterator();
In order for this code to compile, it is important for the
ArrayListIterator
class to implement the
Iterator
interface and for the return type for the
iterator()
method to use the interface rather than the class name. These changes
have been incorporated into the final version of the class. It turns out that there are
other interfaces to consider as well, but we will save that discussion until the end of
the next chapter. At that time we will see a final version of the
ArrayList
class that
is even closer to the built-in version written by Sun.
Chapter Summary
In this chapter we implemented an array list class to store
lists of integers.
left respectively to account for the newly added or
removed element.
Collection classes have two views: the external view seen by
client code and the internal view seen by the implementer.
Our array list has preconditions that the client will not
pass an illegal capacity on construction or illegal indexes
when accessing elements. If the client does try to do so,
Java will throw an exception.
An array list uses an unfilled array and a
size
field in
which the first
size
elements are meaningful and the rest
are empty zeroes that are not considered to be part of the
list. The entire array length represents its capacity for
storing values.
Our subsequent versions of the list class resize to a larger
capacity when the array becomes full.
The array list has an iterator for examining its elements in
sequence.
When we add values to or remove values from the front or
middle of an array list, we must shift the values right or
Search WWH ::
Custom Search