img
Method
Description
boolean hasNext( )
Returns true if there are more elements. Other wise, returns false.
E next( )
Returns the next element. Throws NoSuchElementException if there is not
a next element.
void remove( )
Removes the current element. Throws IllegalStateException if an attempt
is made to call remove( ) that is not preceded by a call to next( ).
TABLE 17-8
The Methods Defined by Iterator
bidirectional traversal of a list, and the modification of elements. Iterator and ListIterator
are generic interfaces which are declared as shown here:
interface Iterator<E>
interface ListIterator<E>
Here, E specifies the type of objects being iterated. The Iterator interface declares the methods
shown in Table 17-8. The methods declared by ListIterator are shown in Table 17-9. In both
cases, operations that modify the underlying collection are optional. For example, remove( )
will throw UnsupportedOperationException when used with a read-only collection. Various
other exceptions are possible.
Using an Iterator
Before you can access a collection through an iterator, you must obtain one. Each of the
collection classes provides an iterator( ) method that returns an iterator to the start of
the collection. By using this iterator object, you can access each element in the collection, one
Method
Description
void add(E obj)
Inser ts obj into the list in front of the element that will be returned
by the next call to next( ).
boolean hasNext( )
Returns true if there is a next element. Other wise, returns false.
boolean hasPrevious( )
Returns true if there is a previous element. Other wise, returns false.
E next( )
Returns the next element. A NoSuchElementException is thrown
if there is not a next element.
int nextIndex( )
Returns the index of the next element. If there is not a next element,
returns the size of the list.
E previous( )
Returns the previous element. A NoSuchElementException is thrown
if there is not a previous element.
int previousIndex( )
Returns the index of the previous element. If there is not a previous
element, returns -1.
void remove( )
Removes the current element from the list. An IllegalStateException
is thrown if remove( ) is called before next( ) or previous( ) is invoked.
void set(E obj)
Assigns obj to the current element. This is the element last returned
by a call to either next( ) or previous( ).
TABLE 17-9
The Methods Defined by ListIterator
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home