Rolling Your Own Iterator

Problem

You have your own data structure, but you want to publish the data as an Iterator to provide generic access to it You need to write your own Iterator.

Solution

Just implement (or provide an inner class that implements) the Iterator (or Enumeration ) interface.

Discussion

To make data from one part of your program available in a storage-independent way to other parts of the code, generate an Iterator. Here is a short program that constructs, upon request, an Iterator for some data that it is storing, in this case in an array. The Iterator interface has only three methods: hasNext( ) , next( ), and remove( ).

import java.util.*;

/** Demonstrate the Iterator interface (new in 1.2).
 */
public class IterDemo implements Iterator {
    protected String[] data = { "one", "two", "three" };

    protected int index = 0;

    /** Returns true if not at the end, i.e., if next(  ) will return
     *  an element. Returns false if next(  ) will throw an exception.
     */
    public boolean hasNext(  ) {
        return (index < data.length);
    }

    /** Returns the next element from the data */
    public Object next(  ) {
        if (index >= data.length)
            throw new IndexOutOfBoundsException(
                "only " + data.length + " elements");
        return data[index++];
    }

    /** Remove the object that next(  ) just returned.
     * An Iterator is not required to support this interface,
     * and we certainly don't. :-)
     */
    public void remove(  ) {
        throw new UnsupportedOperationException(
            "This demo does not implement the remove method");
    }

    /** Simple tryout */
    public static void main(String unused[]) {
        IterDemo it = new IterDemo(  );
        while (it.hasNext(  ))
            System.out.println(it.next(  ));
    }
}

The comments above the remove( ) method remind me of an interesting point. This interface introduces something new to Java, the optional method . Since there is no syntax for this and they didn’t want to introduce any new syntax, the developers of the Collections Framework decided on an implementation using existing syntax. If they are not implemented, the optional methods are required to throw an UnsupportedOperationException if they ever get called. My remove( ) method does this. Note that UnsupportedOperationException is subclassed from RunTimeException, so it is not required to be declared or caught.

This code is unrealistic in several ways, but it does show the syntax and how the Iterator interface works. In real code, the Iterator and the data are usually separate objects (the Iterator might be an inner class from the data store class). Also, you don’t even need to write this code for an array; you can just construct an ArrayList object, copy the array elements into it, and ask it to provide the Iterator. However, I believe it’s worth showing this simple example of the internals of an Iterator so you can understand both how it works and how you could provide one for a more sophisticated data structure, should the need arise.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.74.231