ArrayIteratorpublic class ArrayIterator extends Object implements IteratorDe-mystify the Iterator interface, showing how
to write a simple Iterator for an Array of Objects. |
Fields Summary |
---|
protected Object[] | dataThe data to be iterated over. | protected int | index |
Constructors Summary |
---|
public ArrayIterator(Object[] d)Construct an ArrayIterator object.
setData(d);
|
Methods Summary |
---|
public boolean | hasNext()Tell if there are any more elements.
return (index < data.length);
| public java.lang.Object | next()Returns the next element from the data
if (hasNext()) {
return data[index++];
}
throw new IndexOutOfBoundsException("only " + data.length + " elements");
| public void | remove()Remove the object that next() just returned.
An Iterator is not required to support this interface,
and we certainly don't!
throw new UnsupportedOperationException(
"This demo does not implement the remove method");
| public void | setData(java.lang.Object[] d)(Re)set the data array to the given array, and reset the iterator.
this.data = d;
index = 0;
|
|