The best way to find out is to use javap to take a look at the byte code for both loops.
My guess would be that they are (near to) identical.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I was taught that the "correct" way to iterate an ArrayList is to use an Iterator, rather than a "for" loop with a counter. But recently I came across the RandomAccess tagging interface; it suggests that a for loop with a counter may be faster for an ArrayList. Have a look at the API docs for ArrayList and RandomAccess.
In a Java 1.3 and 1.4 environment at work, I've recently started using Keylee's 2nd example style with a for loop and an iterator. I resisted for a while because I thought for loops so often used an int counter or index that it might confuse readers to do something else. I guess getting rid of a line of code and a variable scoped outside the loop finally won me over.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Jul 11, 2001
Originally posted by Jesper Young:
If you are using Java 5 or newer and you don't need explicit access to the iterator, then the shortest and clearest way to iterate over a list is to use the new "for each" syntax
True - it will be the shortest and clearest source code, which is important. I'd again guess that the compiled byte code will be still near to identical.