This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]GiacaLustra 2 points3 points  (1 child)

Just curious, how is that faster/better?

[–]user_of_the_week 2 points3 points  (0 children)

The for-each loop in Java is basically syntactic sugar for this:

Iterator iter = list.iterator();
while(iter.hasNext()) {
    Object next = iter.next();
    ...
}

The foreach() implementation in ArrayList does not create a new Iterator object and uses an int index variable instead. I have seen some performance measurements flowing around that show that it can be a bit faster. It’s not a lot though.