Iterator



This is the collection class.
Note the internal Iterator class that implements the
IIterator interface.
The Iterator will be returned to the client to traverse
the collection without having to know anything about the 
objects contained or how they are contained.
-----------------------------------------------------
class Collection implements ICollection{

    String[] array = {"one","two","three","four","five"};

    public IIterator iterator(){
        return new Iterator(this.array);
    }

    private class Iterator implements IIterator{
        private String[] myArray;
        private int index =0;

        public Iterator(String[] array){
        this.myArray = array;
        }

        public Object getNext(){
            return myArray[index++];
        }

        public boolean hasNext(){
            if(index < myArray.length)
                return true;
            return false;
        }
    }
}
-----------------------------------------------------


The IIterator interface.
-----------------------------------------------------
public interface IIterator{
    public Object getNext();
    public boolean hasNext();
}
-----------------------------------------------------


The ICollection interface.
-----------------------------------------------------
public interface ICollection{
   public IIterator iterator();
}
-----------------------------------------------------


The client class.
-----------------------------------------------------
class Test{

    public static void main(String[] args){
        System.out.println("class: Test, method: main");

        Collection collection = new Collection();

        IIterator iIterator = collection.iterator();

        while(iIterator.hasNext()){
            System.out.println(iIterator.getNext());
        }
    }
}
-----------------------------------------------------


Here is a tar with the above java classes in it. 
iterator.tar