What do you think about the for-each loop?

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
 
D

Dale King

Adam said:
I'm curious about this. How would foreach make nested loops more
confusing if it worked for iterators?

Consider this code which is currently valid:

List<String> list1;
List<String> list2;

for( String s1 : list1 )
{
for( String s2 : list2 )
{
Sytem.out.printf( s1 + s2 );
}
}

And we change that to use iterators instead:

Iterator<String> i1 = list1.iterator();
Iterator<String> i2 = list2.iterator();

for( String s1 : i1 )
{
for( String s2 : i2 )
{
Sytem.out.printf( s1 + s2 );
}
}

This has very different behavior and could likely trip a newbie. Of
course you can fix this by moving the second statement inside the outer
loop and you may say the confusion is minimal, but the point is that no
such confusion exists in the current specification.
 
A

Adam P. Jenkins

Dale said:
Consider this code which is currently valid:

List<String> list1;
List<String> list2;

for( String s1 : list1 )
{
for( String s2 : list2 )
{
Sytem.out.printf( s1 + s2 );
}
}

And we change that to use iterators instead:

Iterator<String> i1 = list1.iterator();
Iterator<String> i2 = list2.iterator();

for( String s1 : i1 )
{
for( String s2 : i2 )
{
Sytem.out.printf( s1 + s2 );
}
}

This has very different behavior and could likely trip a newbie. Of
course you can fix this by moving the second statement inside the outer
loop and you may say the confusion is minimal, but the point is that no
such confusion exists in the current specification.

Sorry, I must slow or something but I don't see the problem with the
second example. What am I missing? My idea of what the Iterator
example would expand to if it were allowed is:

while (i1.hasNext()) {
String s1 = i1.next();
while (i2.hasNext()) {
String s2 = i2.next();
System.out.printf(s1 + s2);
}
}

This seems to be exactly the behavior anyone would expect. What
ambiguity am I missing? Thanks.

Adam
 
J

Joona I Palaste

Sorry, I must slow or something but I don't see the problem with the
second example. What am I missing? My idea of what the Iterator
example would expand to if it were allowed is:
while (i1.hasNext()) {
String s1 = i1.next();
while (i2.hasNext()) {
String s2 = i2.next();
System.out.printf(s1 + s2);
}
}
This seems to be exactly the behavior anyone would expect. What
ambiguity am I missing? Thanks.

AFAIK the problem is that Iterators are stateful creatures. They
remember what object they returned last time their hasNext() method
was called, and the next time, they return the object after that.
In the first example, using Lists, the inner loop would iterate over
all elements of l2 for each element of l1. But in the second example,
using Iterators, the inner loop would only iterate over all elements
of l2 for the first element of l1. On subsequent elements, it would
see that it already is at the end, and not iterate any further.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top