Why bother to write an iterator like this?
<p>I was reading through the Java tutorial on inner class</p>

<p><a href="http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html" rel="nofollow">http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html</a></p>

<p>It explains that in the example "The InnerEvenIterator inner class, which is similar to a standard Java iterator." So I take it that iterators are pretty common in Java?</p>

<p>I came from a C programming background. I don't understand why a simple loop like this</p>

<pre><code>for(i=0;i &lt;SIZE;i+2){
System.System.out.println(arrayOfInts[i]));
}
</code></pre>

<p>got expanded to an iterators (inner class) with two methods. What's the point here?</p>

<pre><code>public class DataStructure {
    //create an array
    private final static int SIZE = 15;
    private int[] arrayOfInts = new int[SIZE];

    public DataStructure() {
        //fill the array with ascending integer values
        for (int i = 0; i &lt; SIZE; i++) {
            arrayOfInts[i] = i;
        }
    }

    public void printEven() {
        //print out values of even indices of the array
        InnerEvenIterator iterator = this.new InnerEvenIterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.getNext() + " ");
        }
    }

//inner class implements the Iterator pattern
    private class InnerEvenIterator {
        //start stepping through the array from the beginning
        private int next = 0;

        public boolean hasNext() {
            //check if a current element is the last in the array
            return (next &lt;= SIZE - 1);
        }

        public int getNext() {
            //record a value of an even index of the array
            int retValue = arrayOfInts[next];
            //get the next even element
            next += 2;
            return retValue;
        }
    }

    public static void main(String s[]) {
        //fill the array with integer values and print out only values of even indices
        DataStructure ds = new DataStructure();
        ds.printEven();
    }
}
</code></pre>
