Java Reference
In-Depth Information
Method
Description
next()
Retrieves the next object in sequence - the same as for the
Iterator
interface.
hasNext()
Returns
true
if there is an object that will be returned by
next()
.
nextIndex()
Returns the index of the object that will be returned by the next call
to
next()
, or returns the number of elements in the list if the
ListIterator
object is at the end of the list.
previous()
Returns the previous object in sequence in the list. You use this
method to run backwards through the list.
hasPrevious()
Returns
true
if the next call to
previous()
will return an object.
previousIndex()
Returns the index of the object that will be returned by the next call
to
previous()
, 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 backwards and forwards through the list.
Calling
previous()
immediately after calling
next()
will return the same element - and
vice versa
.
With a
ListIterator
you can add and replace objects, as well as remove them from the collection.
ListIterator
declares the following methods for this:
Method
Description
remove()
Removes the last object that was retrieved by
next()
or
previous()
. The
UnsupportedOperation
exception is thrown
if the remove operation is not supported for this collection, and an
IllegalStateException
will be thrown if
next()
or
previous()
have not yet been called for the iterator.
add(Object 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 will return the object that was added. The
next call to
previous()
will not be affected. This method throws
an
UnsupportedOperationException
if objects cannot be
added, a
ClassCastException
if the class of the argument
prevents it from being added, and
IllegalOperationException
if there is some other reason why
the add cannot be done.