Forwards, backwards, take your pick
for(Iterator i =
forwards ? things.iterator() :
new ReverseIterator(things);
i.hasNext(); )
{
Thing thing = (Thing)i.next();
if((t.age() < 30) && !t.shipped())
print(t.name());
}
Here we go. We can switch between forward and reverse directions at runtime, without having to change anything else, or duplicate any code.
Does that seem reasonable? Does it seem useful?
The thing to take hold of here is that this ReverseIterator is entirely of our own devising. Although it iterates over a List, it didn't come from the List, we didn't have to ask the list for it. If we take hold of this idea of Iterators as things in their own right, useful things, we can go quite a long way.
All we needed was a little nudge to get us going.
Let's go a bit further, see where it takes us.