How could I use of the loop for?
Hullo! Please, I would like to know how could I change that code using the new loop for(that who has the form for(....... : ...). At least I would like to know how does it go, because I've heard It's very useful.
The code I would like to change is:
.......
Iterator<Long> i = results.iterator();
long firstY = i.next();
long firstX = 1;
int n = 2;
while (i.hasNext()) {
long secondY = i.next();
long secondX = n;
papel.linea(firstX, firstY, secondX, secondY, Color.BLUE);
n++;
firstY = secondY;
firstX = secondX;
.................
I would be very grateful if you could teach me how to change it. Thank you!!
Message was edited by:
julipi_90
[763 byte] By [
julipia] at [2007-10-2 20:59:00]

for(int n = 2;i.hasNext();n++) {
long secondY = i.next();
long secondX = n;
papel.linea(firstX, firstY, secondX, secondY, Color.BLUE);
}
The OP asked for the new for loop (aka for-each), not the old for loop. However, the code doesn't seem to be a very good match for the new for loop because of the initial step.
In general, the new for-loop would replace the Iterator:for(long firstY : results) {
// do stuff
}
However, the "do stuff" part in this case would be a non-obvious translation of what you already have. I doubt adding the for-each loop would make your code more concise or clearer (which is the only advantage of the new for).
I would recommend getting rid of the int n though, it doesn't seem to serve any purpose.