## org.apache.commons.collections.iterators
Class LoopingListIterator

    java.lang.Object
      org.apache.commons.collections.iterators.LoopingListIterator

All Implemented Interfaces:
  java.util.Iterator, java.util.ListIterator, ResettableIterator, ResettableListIterator

    public class LoopingListIterator

extends java.lang.Object
implements ResettableListIterator

A ListIterator that restarts when it reaches the end or when it reaches the beginning.

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.

Since:
  Commons Collections 3.2
Version:
  $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
Author:
  Eric Crampton <ccesc@eonomine.com>

Constructor Summary
`LoopingListIterator(java.util.List list)` Constructor that wraps a list.

Method Summary
`void` `add(java.lang.Object 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.
`java.lang.Object` `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()`.
`java.lang.Object` `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(java.lang.Object obj)` Replaces the last element that was returned by `next()` or `previous()`.
`int` `size()` Gets the size of the list underlying the iterator.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### LoopingListIterator

    public LoopingListIterator(java.util.List list)

  Constructor that wraps a 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.

  Parameters:
    `list` - the list to wrap
  Throws:
    `java.lang.NullPointerException` - if the list it null

Method Detail

### hasNext

    public boolean hasNext()

  Returns whether this iterator has any more elements.

  Returns false only if the list originally had zero elements, or all elements have been `removed`.

  Specified by:
    `hasNext` in interface `java.util.Iterator`
  Specified by:
    `hasNext` in interface `java.util.ListIterator`
  Returns:
    `true` if there are more elements

### next

    public java.lang.Object next()

  Returns the next object in the list.

  If at the end of the list, returns the first element.

  Specified by:
    `next` in interface `java.util.Iterator`
  Specified by:
    `next` in interface `java.util.ListIterator`
  Returns:
    the object after the last element returned
  Throws:
    `java.util.NoSuchElementException` - if there are no elements in the list

### nextIndex

    public int nextIndex()

  Returns the index of the element that would be returned by a subsequent call to `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.

  Specified by:
    `nextIndex` in interface `java.util.ListIterator`
  Returns:
    the index of the element that would be returned if next() were called
  Throws:
    `java.util.NoSuchElementException` - if there are no elements in the list

### hasPrevious

    public boolean hasPrevious()

  Returns whether this iterator has any more previous elements.

  Returns false only if the list originally had zero elements, or all elements have been `removed`.

  Specified by:
    `hasPrevious` in interface `java.util.ListIterator`
  Returns:
    `true` if there are more elements

### previous

    public java.lang.Object previous()

  Returns the previous object in the list.

  If at the beginning of the list, return the last element. Note that in this case, traversal to find that element takes linear time.

  Specified by:
    `previous` in interface `java.util.ListIterator`
  Returns:
    the object before the last element returned
  Throws:
    `java.util.NoSuchElementException` - if there are no elements in the list

### previousIndex

    public int previousIndex()

  Returns the index of the element that would be returned by a subsequent call to `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.

  Specified by:
    `previousIndex` in interface `java.util.ListIterator`
  Returns:
    the index of the element that would be returned if previous() were called
  Throws:
    `java.util.NoSuchElementException` - if there are no elements in the list

### remove

    public void remove()

  Removes the previously retrieved item from the underlying list.

  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.

  Specified by:
    `remove` in interface `java.util.Iterator`
  Specified by:
    `remove` in interface `java.util.ListIterator`
  Throws:
    `java.lang.UnsupportedOperationException` - if the remove method is not supported by the iterator implementation of the underlying list

### add

    public void add(java.lang.Object obj)

  Inserts the specified element into the underlying list.

  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.

  Specified by:
    `add` in interface `java.util.ListIterator`
  Parameters:
    `obj` - the element to insert
  Throws:
    `java.lang.UnsupportedOperationException` - if the add method is not supported by the iterator implementation of the underlying list

### set

    public void set(java.lang.Object obj)

  Replaces the last element that was returned by `next()` or `previous()`.

  This feature is only supported if the underlying list's `List.listIterator()` method returns an implementation that supports it.

  Specified by:
    `set` in interface `java.util.ListIterator`
  Parameters:
    `obj` - the element with which to replace the last element returned
  Throws:
    `java.lang.UnsupportedOperationException` - if the set method is not supported by the iterator implementation of the underlying list

### reset

    public void reset()

  Resets the iterator back to the start of the list.

  Specified by:
    `reset` in interface `ResettableIterator`
  Specified by:
    `reset` in interface `ResettableListIterator`

### size

    public int size()

  Gets the size of the list underlying the iterator.

  Returns:
    the current list size

Copyright © 2001-2008 The Apache Software Foundation. All Rights Reserved.