A
Adam P. Jenkins
Dale said:There is a potential of confusion if it worked for iterators. I can see
some confusion when you start getting into nested loops.
I'm curious about this. How would foreach make nested loops more
confusing if it worked for iterators?
List<String> list1, list2;
// here's what I'd like
for (String s1 : list1.listIterator(3)) {
for (String s2 : list2.listIterator(3)) {
System.out.println(s1 + s2);
}
}
// As compared to what I actually have to do
for (Iterator<String> it1 = list1.listIterator(3);
it1.hasNext(); ) {
String s1 = it1.next();
for (Iterator<String> it2 = list2.listIterator(3);
it2.hasNext(); ) {
String s2 = it2.next();
System.out.println(s1 + s2);
}
}
I'm only using List as an example because it's a well-known class in the
standard library which has a method that returns an Iterator, which
isn't part of the Iterable interface. There are many other such
classes, even in the standard library.
Adam