Suppose that looking up something by index is expensive, while stepping through from element to the next is cheap (say, a long linked-list). The compare the performance of your loop, with the performance of an iterator.
–
Anon.Dec 16 '09 at 5:42

1

Or the case where you don't actually know the size of the list, for example if you're pulling the content data on demand in the next() method. In that case, the Iterator abstraction means you don't have to pull down the entire data set at the beginning in order to work out the size.
–
AshDec 16 '09 at 5:45

5 Answers
5

The idea of an iterator is to decouple how the data is stored (might not be an array) from its consumer (the code that wants to iterate over it).

You are right to say that Iterator is a pretty core concept in the Java class library, so common that from Java5 on there is the for-each loop language feature to support it. With this loop, the user does not even see the Iterator anymore.

One nice thing (the big thing?) that having iterators gets you is the ability to examine different data structures in a uniform way, even if the data structure can't be indexed by an integer. In C++, for example, you can use iterators to run through arrays, and sets, and vectors, and maps, and even your own data structures, all in a conceptually and syntactically uniform way. Even if the map happens to be from Strings to Widgets!

With the C++ approach, using iterators will always be at least as efficient as using any other access mechanism. Java has different priorities. Stephen is right that the code example from the Java tutorial is probably not a good example of why you'd want to have or use iterators.

I agree with Stephen C that this is an illustration of an inner class. I would like to point out that coming to Java from C you will probably notice alot more object oriented things. Depending on your familiarity with the OO paradigm some may seem quite foreign compared to the functional programing style you are used to.

The inner class is simply another example of turning thing like an itterator into an object which represents an iterator. By doing this we gain that layer of abstraction which is so paramount to object oriented programming.

I don't think I would use an inner class in a situation outlined in the linked example.
As you mention, for the example the C style code with the for loops is better.
But I think since the intention of the tutorial is to educate learners about use
of inner classes the example helps in understanding the concept of inner classes
though its practical usage doesn't look relevant.

And as the tutorial mentions, Inner classes are useful in event handling part
in GUI programming. For eventhandling you can register an instance of inner class as
the EventHandler (for an action on button in the GUI) whose method will be invoked
when an action is performed on the button. (Say button clicked) (callback mechanism).
An inner class has access to the private members of the enclosing class and if the event
handling code is smaller (as is in most of the cases) it makes sense to use inner classes
with in GUI code.

In my opinion if the tutorial provided an example of inner class usage with a GUI
code as an example it would have been confusing and wouldn't have served the purpose.
I think it is supposed to be a beginner's tutorial and a person going through it might not
have familiarity in doing GUI development using Java.