Java Reference
In-Depth Information
iterator each time you need to go through the objects in a collection because an iterator is a “use once" ob-
ject.
The iterator that you have seen here is a single-use one-way street — you can go through the objects in
a collection one at a time, in sequence, once, and that's it. This is fine for many purposes but not all, so you
have other possibilities for accessing the entire contents of a collection. You can access the objects in any
collection that implements the Iterable<T> interface directly using the collection-based for loop. If this is
not enough, there's another kind of iterator that is more flexible than the one you have seen — called a list
iterator .
List Iterators
The java.util.ListIterator<T> interface declares methods that you can use to traverse a collection of
objects backward or forward. You don't have to choose a particular direction either. You can change from
forward to backward and vice versa at any time, so an object can be retrieved more than once.
The ListIterator<> interface extends the Iterator<> interface type so the iterator methods you have
already seen and used still apply. The additional methods that the ListIterator<> interface declares for
moving through a collection are:
int nextIndex() : Returns the index of the object that is returned by the next call to next() as
type int , or returns the number of elements in the list if the ListIterator<> object is at the end
of the list.
T previous() : Returns the previous object in sequence in the list. You use this method to run
backward through the list.
boolean hasPrevious() : Returns true if the next call to previous() returns an object.
int previousIndex()() : Returns the index of the object that is returned by the next call to pre-
vious() , or returns −1 if the ListIterator<> object is at the beginning of the list.
You can alternate between calls to next() and previous() to go backward and forward through the list.
Calling previous() immediately after calling next() , or vice versa , returns the same element.
In addition to the remove() method, a ListIterator<> object provides methods that can add and re-
place objects:
void add(T obj): Adds the argument immediately before the object that would be returned by
the next call to next() , and after the object that would be returned by the next call to previous() .
The call to next() after the add() operation returns the object that was added. The next call to
previous() is not affected. This method throws an UnsupportedOperationException if objects
cannot be added, a ClassCastException if the class of the argument prevents it from being ad-
ded, and IllegalOperationException if there is some other reason why the add cannot be done.
void set(T obj) : Replaces the last object retrieved by a call to next() or previous() . If neither
next() nor previous() have been called, or add() or remove() have been called most recently,
an IllegalStateException is thrown. If the set() operation is not supported for this collec-
tion, an UnsupportedOperationException is thrown. If the class of the reference passed as an
argument prevents the object from being stored, a ClassCastException is thrown. If some other
characteristic of the argument prevents it from being stored, an IllegalArgumentException is
thrown.
Now that you know more about iterators, you need to find out a bit about the collection classes them-
selves to make use of them.
Search WWH ::




Custom Search