com.darwinsys.util
Class ArrayIterator

java.lang.Object
  extended by com.darwinsys.util.ArrayIterator
All Implemented Interfaces:
java.util.Iterator

public class ArrayIterator
extends java.lang.Object
implements java.util.Iterator

De-mystify the Iterator interface, showing how to write a simple Iterator for an Array of Objects.

Version:
$Id: ArrayIterator.java,v 1.12 2006/04/11 22:27:42 ian Exp $
Author:
Ian Darwin, http://www.darwinsys.com/

Field Summary
protected  java.lang.Object[] data
          The data to be iterated over.
protected  int index
           
 
Constructor Summary
ArrayIterator(java.lang.Object[] d)
          Construct an ArrayIterator object.
 
Method Summary
 boolean hasNext()
          Tell if there are any more elements.
 java.lang.Object next()
          Returns the next element from the data
 void remove()
          Remove the object that next() just returned.
 void setData(java.lang.Object[] d)
          (Re)set the data array to the given array, and reset the iterator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

data

protected java.lang.Object[] data
The data to be iterated over.


index

protected int index
Constructor Detail

ArrayIterator

public ArrayIterator(java.lang.Object[] d)
Construct an ArrayIterator object.

Parameters:
d - The array of objects to be iterated over.
Method Detail

setData

public void setData(java.lang.Object[] d)
(Re)set the data array to the given array, and reset the iterator.

Parameters:
d - The array of objects to be iterated over.

hasNext

public boolean hasNext()
Tell if there are any more elements.

Specified by:
hasNext in interface java.util.Iterator
Returns:
true if not at the end, i.e., if next() will succeed.

next

public java.lang.Object next()
Returns the next element from the data

Specified by:
next in interface java.util.Iterator

remove

public void remove()
Remove the object that next() just returned. An Iterator is not required to support this interface, and we certainly don't!

Specified by:
remove in interface java.util.Iterator


Copyright © 1996-2004 Ian F. Darwin. See license.html for usage license.