org.clapper.util.misc
Class ArrayIterator<T>

java.lang.Object
  extended by org.clapper.util.misc.ArrayIterator<T>
All Implemented Interfaces:
java.util.Iterator<T>

public class ArrayIterator<T>
extends java.lang.Object
implements java.util.Iterator<T>

The ArrayIterator class provides a bridge between an array of objects and an Iterator. It's useful in cases where you have an array, but you need an Iterator; using an instance of ArrayIterator saves copying the array's contents into a Collection, just to get an Iterator.

Version:
$Revision: 6735 $
Author:
Copyright © 2004-2007 Brian M. Clapper
See Also:
Iterator

Constructor Summary
ArrayIterator(T[] array)
          Allocate a new ArrayIterator object that will iterate over the specified array of objects.
ArrayIterator(T[] array, int index)
          Allocate a new ArrayIterator object that will iterate over the specified array of objects, starting at a particular index.
 
Method Summary
 int getNextIndex()
          Get the index of the next element to be retrieved.
 boolean hasNext()
          Determine whether the underlying Iterator has more elements.
 T next()
          Get the next element from the underlying array.
 T previous()
          Get the previous element from the underlying array.
 void remove()
          Required by the Iterator interface, but not supported by this class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayIterator

public ArrayIterator(T[] array)
Allocate a new ArrayIterator object that will iterate over the specified array of objects.

Parameters:
array - The array over which to iterate

ArrayIterator

public ArrayIterator(T[] array,
                     int index)
Allocate a new ArrayIterator object that will iterate over the specified array of objects, starting at a particular index. The index isn't checked for validity until next() is called.

Parameters:
array - The array over which to iterate
index - The index at which to start
Method Detail

getNextIndex

public int getNextIndex()
Get the index of the next element to be retrieved. This index value might be past the end of the array.

Returns:
the index

hasNext

public boolean hasNext()
Determine whether the underlying Iterator has more elements.

Specified by:
hasNext in interface java.util.Iterator<T>
Returns:
true if and only if a call to next() will return an element, false otherwise.
See Also:
next()

next

public T next()
       throws java.util.NoSuchElementException
Get the next element from the underlying array.

Specified by:
next in interface java.util.Iterator<T>
Returns:
the next element from the underlying array
Throws:
java.util.NoSuchElementException - No more elements exist
See Also:
previous(), Iterator.next()

previous

public T previous()
           throws java.util.NoSuchElementException
Get the previous element from the underlying array. This method decrements the iterator's internal index by one, and returns the corresponding element.

Returns:
the previous element from the underlying array
Throws:
java.util.NoSuchElementException - Attempt to move internal index before the first array element
See Also:
next()

remove

public void remove()
Required by the Iterator interface, but not supported by this class.

Specified by:
remove in interface java.util.Iterator<T>
Throws:
java.lang.UnsupportedOperationException - unconditionally


Copyright © 2004-2007 Brian M. Clapper. All Rights Reserved.