Class IteratorExample

java.lang.Object
  extended byIteratorExample

public class IteratorExample
extends java.lang.Object

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:

  1. Define a public method named iterator that returns a new Iterator instance;

  2. Define a protected inner class named "AnIterator" that implements the Iterator interface; this entails implementing the hasNext, next, and remove methods.
In this style of implementation, all the iterator method does is return "new AnIterator()". The implementation of the next method keeps track of which element needs to be returned next and returns that element each time next called.

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

items

protected int[] items
This' data rep is a 5-element array

Constructor Detail

IteratorExample

public IteratorExample()
Construct this and initialize its data.

Method Detail

iterator

public java.util.Iterator iterator()
Return an iterator for this.


main

public static void main(java.lang.String[] args)
Construct an IteratorExample and exercise its iterator.