public class LoopingListIterator<E> extends Object implements ResettableListIterator<E>
The iterator will loop continuously around the provided list,
unless there are no elements in the collection to begin with, or
all of the elements have been removed
.
Concurrent modifications are not directly supported, and for most collection implementations will throw a ConcurrentModificationException.
Constructor and Description |
---|
LoopingListIterator(List<E> list)
Constructor that wraps a list.
|
Modifier and Type | Method and Description |
---|---|
void |
add(E obj)
Inserts the specified element into the underlying list.
|
boolean |
hasNext()
Returns whether this iterator has any more elements.
|
boolean |
hasPrevious()
Returns whether this iterator has any more previous elements.
|
E |
next()
Returns the next object in the list.
|
int |
nextIndex()
Returns the index of the element that would be returned by a
subsequent call to
next() . |
E |
previous()
Returns the previous object in the list.
|
int |
previousIndex()
Returns the index of the element that would be returned by a
subsequent call to
previous() . |
void |
remove()
Removes the previously retrieved item from the underlying list.
|
void |
reset()
Resets the iterator back to the start of the list.
|
void |
set(E obj)
Replaces the last element that was returned by
next() or
previous() . |
int |
size()
Gets the size of the list underlying the iterator.
|
public LoopingListIterator(List<E> list)
There is no way to reset a ListIterator instance without recreating it from the original source, so the List must be passed in and a reference to it held.
list
- the list to wrapNullPointerException
- if the list it nullpublic boolean hasNext()
Returns false only if the list originally had zero elements, or
all elements have been removed
.
public E next()
If at the end of the list, returns the first element.
next
in interface Iterator<E>
next
in interface ListIterator<E>
NoSuchElementException
- if there are no elements in the listpublic int nextIndex()
next()
.
As would be expected, if the iterator is at the physical end of the underlying list, 0 is returned, signifying the beginning of the list.
nextIndex
in interface ListIterator<E>
NoSuchElementException
- if there are no elements in the listpublic boolean hasPrevious()
Returns false only if the list originally had zero elements, or
all elements have been removed
.
hasPrevious
in interface ListIterator<E>
hasPrevious
in interface OrderedIterator<E>
true
if there are more elementspublic E previous()
If at the beginning of the list, return the last element. Note that in this case, traversal to find that element takes linear time.
previous
in interface ListIterator<E>
previous
in interface OrderedIterator<E>
NoSuchElementException
- if there are no elements in the listpublic int previousIndex()
previous()
.
As would be expected, if at the iterator is at the physical beginning of the underlying list, the list's size minus one is returned, signifying the end of the list.
previousIndex
in interface ListIterator<E>
NoSuchElementException
- if there are no elements in the listpublic void remove()
This feature is only supported if the underlying list's
iterator
method returns an implementation
that supports it.
This method can only be called after at least one next()
or previous()
method call. After a removal, the remove
method may not be called again until another next()
or
previous()
has been performed. If the reset()
is
called, then remove may not be called until next()
or
previous()
is called again.
remove
in interface Iterator<E>
remove
in interface ListIterator<E>
UnsupportedOperationException
- if the remove method is
not supported by the iterator implementation of the underlying
listpublic void add(E obj)
The element is inserted before the next element that would be
returned by next()
, if any, and after the next element
that would be returned by previous()
, if any.
This feature is only supported if the underlying list's
List.listIterator()
method returns an implementation
that supports it.
add
in interface ListIterator<E>
obj
- the element to insertUnsupportedOperationException
- if the add method is not
supported by the iterator implementation of the underlying listpublic void set(E obj)
next()
or
previous()
.
This feature is only supported if the underlying list's
List.listIterator()
method returns an implementation
that supports it.
set
in interface ListIterator<E>
obj
- the element with which to replace the last element returnedUnsupportedOperationException
- if the set method is not
supported by the iterator implementation of the underlying listpublic void reset()
reset
in interface ResettableIterator<E>
public int size()
Copyright © 2001–2015 The Apache Software Foundation. All rights reserved.