Java: why can't iterate over an iterator?

Posted by noamtm on Stack Overflow See other posts from Stack Overflow or by noamtm
Published on 2010-04-08T07:29:39Z Indexed on 2010/04/08 7:33 UTC
Read the original article Hit count: 320

Filed under:
|
|

I read http://stackoverflow.com/questions/839178/why-is-javas-iterator-not-an-iterable and http://stackoverflow.com/questions/27240/why-arent-enumerations-iterable, but I still don't understand why this:

void foo(Iterator<X> it) {
  for (X x : it) {
    bar(x);
    baz(x);
  }
}

was not made possible. In other words, unless I'm missing something, the above could have been nice and valid syntactic sugar for:

void foo(Iterator<X> it) {
  for (X x; it.hasNext();) {
    x = it.next();
    bar(x);
    baz(x);
  }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about iterator