|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectIteratorExample
Class IteratorExample illustrates the basic design and implementation of an iterator, as defined by the Java Iterator interface. The implementation in this example is typical of that used in JFC library classes, e.g., as in the implementation of AbstractList.iterator.
The point of an iterator is to deliver all of the elements of a collection class, one by one. Each time the Iterator.next method is called, the next collection element is returned. The Iterator.hasNext method returns true as long as there are more elements to be produced by the iterator. The collection class need not be part of the java.util.Collection hierarchy, but simply contain zero or more elements that can be be generated one by one to whoever needs them.
The IteratorExample class is a very simple collection of five integer elements, stored in a five-element array. An iterator is defined for this class as follows:
Users of the iterator call its hasNext and next methods to access all of the elements of the IteratorExample class. A typical use of is the following:
IteratorExample example = new IteratorExample(); Iterator i; for (i = example.iterator(); i.hasNext(); ) { System.out.println(i.next()); }This is the sample code in the ExampleIterator.main method.
Nested Class Summary | |
protected class |
IteratorExample.AnIterator
Inner iterator class that defines the iterator instance returned by this.iterator. |
Field Summary | |
protected int[] |
items
This' data rep is a 5-element array |
Constructor Summary | |
IteratorExample()
Construct this and initialize its data. |
Method Summary | |
java.util.Iterator |
iterator()
Return an iterator for this. |
static void |
main(java.lang.String[] args)
Construct an IteratorExample and exercise its iterator. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected int[] items
Constructor Detail |
public IteratorExample()
Method Detail |
public java.util.Iterator iterator()
public static void main(java.lang.String[] args)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |