Class ReverseListIterator<E>
- java.lang.Object
-
- org.apache.commons.collections4.iterators.ReverseListIterator<E>
-
- All Implemented Interfaces:
java.util.Iterator<E>,java.util.ListIterator<E>,OrderedIterator<E>,ResettableIterator<E>,ResettableListIterator<E>
public class ReverseListIterator<E> extends java.lang.Object implements ResettableListIterator<E>
Iterates backwards through a List, starting with the last element and continuing to the first. This is useful for looping around a list in reverse order without needing to actually reverse the list.The first call to
next()will return the last element from the list, and so on. ThehasNext()method works in concert with thenext()method as expected. However, thenextIndex()method returns the correct index in the list, thus it starts high and reduces as the iteration continues. The previous methods work similarly.- Since:
- 3.2
-
-
Constructor Summary
Constructors Constructor Description ReverseListIterator(java.util.List<E> list)Constructor that wraps a list.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(E obj)Adds a new element to the list between the next and previous elements.booleanhasNext()Checks whether there is another element.booleanhasPrevious()Checks whether there is a previous element.Enext()Gets the next element.intnextIndex()Gets the index of the next element.Eprevious()Gets the previous element.intpreviousIndex()Gets the index of the previous element.voidremove()Removes the last returned element.voidreset()Resets the iterator back to the start (which is the end of the list as this is a reversed iterator)voidset(E obj)Replaces the last returned element.
-
-
-
Constructor Detail
-
ReverseListIterator
public ReverseListIterator(java.util.List<E> list)
Constructor that wraps a list.- Parameters:
list- the list to create a reversed iterator for- Throws:
java.lang.NullPointerException- if the list is null
-
-
Method Detail
-
hasNext
public boolean hasNext()
Checks whether there is another element.
-
next
public E next()
Gets the next element. The next element is the previous in the list.
-
nextIndex
public int nextIndex()
Gets the index of the next element.- Specified by:
nextIndexin interfacejava.util.ListIterator<E>- Returns:
- the index of the next element in the iterator
-
hasPrevious
public boolean hasPrevious()
Checks whether there is a previous element.- Specified by:
hasPreviousin interfacejava.util.ListIterator<E>- Specified by:
hasPreviousin interfaceOrderedIterator<E>- Returns:
- true if there is a previous element
-
previous
public E previous()
Gets the previous element. The next element is the previous in the list.- Specified by:
previousin interfacejava.util.ListIterator<E>- Specified by:
previousin interfaceOrderedIterator<E>- Returns:
- the previous element in the iterator
-
previousIndex
public int previousIndex()
Gets the index of the previous element.- Specified by:
previousIndexin interfacejava.util.ListIterator<E>- Returns:
- the index of the previous element in the iterator
-
remove
public void remove()
Removes the last returned element.
-
set
public void set(E obj)
Replaces the last returned element.- Specified by:
setin interfacejava.util.ListIterator<E>- Parameters:
obj- the object to set- Throws:
java.lang.UnsupportedOperationException- if the list is unmodifiablejava.lang.IllegalStateException- if the iterator is not in a valid state for set
-
add
public void add(E obj)
Adds a new element to the list between the next and previous elements.- Specified by:
addin interfacejava.util.ListIterator<E>- Parameters:
obj- the object to add- Throws:
java.lang.UnsupportedOperationException- if the list is unmodifiablejava.lang.IllegalStateException- if the iterator is not in a valid state for set
-
reset
public void reset()
Resets the iterator back to the start (which is the end of the list as this is a reversed iterator)- Specified by:
resetin interfaceResettableIterator<E>
-
-