No, that's not correct. In java.util.Iterator, the next() method does not
modify the Iterator, it returns a new Iterator.
Seeing as I fundamentally agree that C++'s iterators, which are a
generalization of pointers, are a very solid concept and are very easy
to optimize back to being pointers when necessary, it's rather galling
to see you go so wildly wrong.
For your consideration, the following Java source code:
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class FunWithIterators {
public static void main (String[] args) {
List<String> list = Arrays.asList ("One", "Two", "Three");
Iterator<String> i = list.iterator ();
System.out.printf ("i is %s (identity %d)\n", i, System
.identityHashCode (i));
String next = i.next ();
System.out.printf ("i.next() returned %s\n", next);
System.out.printf ("i is %s (identity %d)\n", i, System
.identityHashCode (i));
next = i.next ();
System.out.printf ("i.next() returned %s\n", next);
System.out.printf ("i is %s (identity %d)\n", i, System
.identityHashCode (i));
}
}
I'd like you to explain how the output of this program squares with
the statements you've made. For reference, public static int
System.identityHashCode(Object) is, for programs with fewer than 2**32
live objects, a unique identifier for the object passed in. You can
trust that, if it returns the same number for two references, that
those references point to the same object.
The signature of Iterator.next() in Java is, in full,
public T next ();
where T is the type of the values being iterated over. It is not,
public Iterator<T> next();
and there is no corresponding
public T getValue();
Iterators in Java act as cursors, and are mutated by access.
I'm not convinced either language's primary Iterator implementation --
C++'s pointer generalization, or Java's cursors -- is all that great.
I've a preference for intrinsic iteration, as with Smalltalk, Ruby, or
most functional languages, where a functor to apply to each element is
passed to the collection, which then applies it. That you can't
mutate the collection during iteration (easily) this way is a feature,
not a bug; there are other tools that are better for that.
-o
(And really, can't language trolls find something juicier to argue
about?)