Class ReverseListIterator
- java.lang.Object
-
- org.apache.commons.collections.iterators.ReverseListIterator
-
- All Implemented Interfaces:
Iterator
,ListIterator
,ResettableIterator
,ResettableListIterator
public class ReverseListIterator extends Object implements ResettableListIterator
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:
- Commons Collections 3.2
- Version:
- $Revision: $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
- Author:
- Serge Knystautas, Stephen Colebourne
-
-
Constructor Summary
Constructors Constructor Description ReverseListIterator(List list)
Constructor that wraps a list.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(Object obj)
Adds a new element to the list between the next and previous elements.boolean
hasNext()
Checks whether there is another element.boolean
hasPrevious()
Checks whether there is a previous element.Object
next()
Gets the next element.int
nextIndex()
Gets the index of the next element.Object
previous()
Gets the previous element.int
previousIndex()
Gets the index of the previous element.void
remove()
Removes the last returned element.void
reset()
Resets the iterator back to the start (which is the end of the list as this is a reversed iterator)void
set(Object obj)
Replaces the last returned element.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Iterator
forEachRemaining
-
-
-
-
Constructor Detail
-
ReverseListIterator
public ReverseListIterator(List list)
Constructor that wraps a list.- Parameters:
list
- the list to create a reversed iterator for- Throws:
NullPointerException
- if the list is null
-
-
Method Detail
-
hasNext
public boolean hasNext()
Checks whether there is another element.- Specified by:
hasNext
in interfaceIterator
- Specified by:
hasNext
in interfaceListIterator
- Returns:
- true if there is another element
-
next
public Object next()
Gets the next element. The next element is the previous in the list.- Specified by:
next
in interfaceIterator
- Specified by:
next
in interfaceListIterator
- Returns:
- the next element in the iterator
-
nextIndex
public int nextIndex()
Gets the index of the next element.- Specified by:
nextIndex
in interfaceListIterator
- Returns:
- the index of the next element in the iterator
-
hasPrevious
public boolean hasPrevious()
Checks whether there is a previous element.- Specified by:
hasPrevious
in interfaceListIterator
- Returns:
- true if there is a previous element
-
previous
public Object previous()
Gets the previous element. The next element is the previous in the list.- Specified by:
previous
in interfaceListIterator
- Returns:
- the previous element in the iterator
-
previousIndex
public int previousIndex()
Gets the index of the previous element.- Specified by:
previousIndex
in interfaceListIterator
- Returns:
- the index of the previous element in the iterator
-
remove
public void remove()
Removes the last returned element.- Specified by:
remove
in interfaceIterator
- Specified by:
remove
in interfaceListIterator
- Throws:
UnsupportedOperationException
- if the list is unmodifiableIllegalStateException
- if there is no element to remove
-
set
public void set(Object obj)
Replaces the last returned element.- Specified by:
set
in interfaceListIterator
- Parameters:
obj
- the object to set- Throws:
UnsupportedOperationException
- if the list is unmodifiableIllegalStateException
- if the iterator is not in a valid state for set
-
add
public void add(Object obj)
Adds a new element to the list between the next and previous elements.- Specified by:
add
in interfaceListIterator
- Parameters:
obj
- the object to add- Throws:
UnsupportedOperationException
- if the list is unmodifiableIllegalStateException
- 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:
reset
in interfaceResettableIterator
- Specified by:
reset
in interfaceResettableListIterator
-
-