Iterator<String> iterator=...
//right way
for (Iterator<String> i = iterator; i.hasNext(); ){
System.out.println(i.next());
}
//why can't?
for(String i:iterator){
}
Reference:
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html
You can do more compactly:
The syntax is only for Iterables (and arrays), not for their Iterators directly (and also not for Enumerations).
Why not? I don’t know… Maybe too much complexity/effort (in the compiler implementation) for a “rare” case. Or edge-cases that would cause trouble (such as an Iterable that is also an Iterator, I think some people make such beasts).
Maybe try libraries like Google Guava to get some more convenient ways to work with Iterators, Collections, and friends.