org.apache.commons.collections.iterators

Class ReverseListIterator

Implemented Interfaces:
Iterator, ListIterator, ResettableIterator, ResettableListIterator

public class ReverseListIterator
extends java.lang.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. The hasNext() method works in concert with the next() method as expected. However, the nextIndex() method returns the correct index in the list, thus it starts high and reduces as the iteration continues. The previous methods work similarly.

Version:
$Revision: $ $Date: 2006-05-12 23:52:43 +0100 (Fri, 12 May 2006) $
Authors:
Serge Knystautas
Stephen Colebourne
Since:
Commons Collections 3.2

Constructor Summary

ReverseListIterator(List list)
Constructor that wraps a list.

Method Summary

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.

Constructor Details

ReverseListIterator

public ReverseListIterator(List list)
Constructor that wraps a list.
Parameters:
list - the list to create a reversed iterator for

Method Details

add

public void add(Object obj)
Adds a new element to the list between the next and previous elements.
Parameters:
obj - the object to add

hasNext

public boolean hasNext()
Checks whether there is another element.
Returns:
true if there is another element

hasPrevious

public boolean hasPrevious()
Checks whether there is a previous element.
Returns:
true if there is a previous element

next

public Object next()
Gets the next element. The next element is the previous in the list.
Returns:
the next element in the iterator

nextIndex

public int nextIndex()
Gets the index of the next element.
Returns:
the index of the next element in the iterator

previous

public Object previous()
Gets the previous element. The next element is the previous in the list.
Returns:
the previous element in the iterator

previousIndex

public int previousIndex()
Gets the index of the previous element.
Returns:
the index of the previous element in the iterator

remove

public void remove()
Removes the last returned element.

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 interface ResettableListIterator
reset in interface ResettableIterator

set

public void set(Object obj)
Replaces the last returned element.
Parameters:
obj - the object to set

Copyright © 2001-2015 Apache Software Foundation. All Rights Reserved.