Java Reference
In-Depth Information
The Interface ListIterator
15.32
The Java Class Library provides a second interface for iterators— ListIterator —in the package
java.util . This type of iterator enables you to traverse a list in either direction and to modify the list
during the iteration. In addition to the three methods hasNext , next , and remove that the interface
Iterator specifies, ListIterator contains methods such as hasPrevious , previous , add , and set .
We begin by looking at the interface ListIterator , which is shown in Listing 15-7.
LISTING 15-7
Java's interface java.util.ListIterator
package java.util;
public interface ListIterator<T> extends Iterator<T>
{
/** Detects whether this iterator has gone beyond the last
entry in the list.
@return true if the iterator has another entry to return when
traversing the list forward; otherwise returns false */
public boolean hasNext();
/** Retrieves the next entry in the list and
advances this iterator by one position.
@return a reference to the next entry in the iteration,
if one exists
@throws NoSuchElementException if the iterator had reached the
end already, that is, if hasNext() is false */
public T next();
/** Removes from the list the last entry that either next()
or previous() has returned.
Precondition: next() or previous() has been called, but the
iterator's remove() or add() method has not been called
since then. That is, you can call remove only once per
call to next() or previous(). The list has not been altered
during the iteration except by calls to the iterator ' s
remove(), add(), or set() methods.
@throws IllegalStateException if next() or previous() has not
been called, or if remove() or add() has been called
already after the last call to next() or previous()
@throws UnsupportedOperationException if the iterator does not
permit a remove operation */
public void remove(); // Optional method
// The previous three methods are in the interface Iterator; they are
// duplicated here for reference and to show new behavior for remove.
/** Detects whether this iterator has gone before the first
entry in the list.
 
 
Search WWH ::




Custom Search