Iterator pattern/iterator class I have implemented a simple iterator. Just want to check if I have missed anything.

interface iterator&lt;E&gt;
    {
        boolean hasNext();

        E next();
    }

    class Iterator&lt;E&gt; implements iterator
    {
        E[] arr;

        int curPos = 0;

        @Override
        public boolean hasNext()
        {
            if (curPos &gt;= arr.length || arr[curPos] == null) 
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        @Override
        public E next()
        {
            if(hasNext())
            {
                E res = arr[curPos];
                curPos++;
                return arr[curPos+1];
            }
            else
            {
                throw new runtimeException("No next element available !");
            }
        }

    }


Questions:


Why should I not use Object instead of E/template everywhere ?  
How do I make an iterator for a binary tree.


I assume apart from the above, I would have to implement a traversal algorithm and try to find if the next "traversal" successor exist?
I would write an iterator for arrays that way:

import java.util.Iterator;
import java.util.NoSuchElementException;

public class ArrayIterator&lt;E&gt; implements Iterator&lt;E&gt; {

    private final E[] arr;
    private int currentIndex = 0;

    public ArrayIterator(E... arr) {
        if(arr == null) {
            throw new IllegalArgumentException("Array is null");
        }
        this.arr = arr;
    }

    public boolean hasNext() {
        return currentIndex &lt; arr.length;
    }

    public E next() {
        if (! hasNext()) {
            throw new NoSuchElementException();
        }
        return arr[currentIndex++];
    }

    public void remove() {
        throw new UnsupportedOperationException("Not supported.");
    }

}


To your second question: I assume you want to traverse the tree in-order, and that the nodes have no link to the parent, only to the children. Then you need a node list from the root node to the current node, which can store if a node was already visited or not, too. 

Then you go left as deep as possible, storing the path in the node list. This is your first current node. When you need the next one, take its parent (from the node list).  Next would be the right children of that node (if there are any) or its own parent. The best way to understand this is to take a sheet of paper, draw different trees, and look how the traversal must work.