- Type Parameters:
E
- The type of the elements being iterated over
- All Superinterfaces:
Iterator<E>
A one-way-only iterator that, in practice, still provides the same functionality
as ListIterator. A
WiredIterator
lets you flip the direction
of the iteration through the turn()
method. Where applicable, all
operations are relative to the direction of the traversal. This includes
operations like next()
,
insertBefore()
and
insertAfter()
. So, for example, with the direction
reversed, a call to next()
takes you closer to the beginning of the list
and further away from the end. A WiredIterator
does not keep track of the
index of the current element, as does a ListIterator
. This makes it less
vulnerable to concurrent modifications, as it only cares about the element
directly ahead of it.- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionint
index()
Returns the index of the current element.void
insertAfter
(E value) Inserts a new element just after the current element.void
insertBefore
(E value) Inserts a new element just before the current element.peek()
Returns the value that would be returned by a call toIterator.next()
without actually moving towards the next element.void
remove()
Removes the current element from the underlying list.void
Sets the value of the current element.turn()
Flips the direction of the iteration.value()
Returns the value of the current element.Methods inherited from interface java.util.Iterator
forEachRemaining, hasNext, next
-
Method Details
-
set
Sets the value of the current element. AnIllegalStateException
is thrown ifIterator.next()
has not been called yet.- Parameters:
newVal
- The new value for the element.
-
value
E value()Returns the value of the current element.- Returns:
- the value of the current element
-
peek
E peek()Returns the value that would be returned by a call toIterator.next()
without actually moving towards the next element. ANoSuchElementException
is thrown if the iterator has arrived at the last element.- Returns:
- The value that would be returned by a call to
next()
.
-
insertBefore
Inserts a new element just before the current element. Note that the list must contain at least one element to begin with.- Parameters:
value
- the value to insert
-
insertAfter
Inserts a new element just after the current element. A subsequent call toIterator.next()
would make the new element the current element. Note that the list must contain at least one element to begin with.- Parameters:
value
- the value to insert
-
remove
void remove()Removes the current element from the underlying list. Contrary to the specification forIterator
, this method multiple times in a row. Before removing the element, theWiredIterator
will move back to the preceding element. Thus, after the removal, the preceding element is the current element again, and a call tonext()
will make the element after the removed element the current element. -
index
int index()Returns the index of the current element. You must have callednext()
at least once before you can call this method. The index is calculated on demand by starting from the first element of the list and advancing until the current element is encountered. Thus, for large lists this method is very inefficient compared to the equivalent methods inListIterator
.- Returns:
- the index of the current element
-
turn
WiredIterator<E> turn()Flips the direction of the iteration. The returnedIterator
is initialized to be at the same element as thisIterator
. You must have calledIterator.next()
at least once before you can callturn()
. AnIllegalStateException
is thrown ifIterator.next()
has not been called yet.- Returns:
- A
WiredIterator
that the traverses the list in the opposite direction.
-