Optimization on for loops with ArrayLists

arraylist, java

Solution

Firstly, what do they mean by "hand-written counted loop"?

I assume they mean

for(int i=0;i<list.size();i++) {
    list.get(i);
}

Secondly, why is it that this holds true only for arraylists and not the other collections?

ArrayList supports efficient random access and removing the Iterator can make a small improvement. (or a large relative improvement if you have a loop which doesn't do anything else)

For other collections e.g. LinkedList, it faster to use an Iterator because `get(n)` is slower. For Set there is no `get(n)`

Problem

From this article, hand-written counted loop is about 3x faster than an enhanced for loop for iterating over arraylists. Firstly, what do they mean by "hand-written counted loop"? They didn't explicitly elaborate what this means. Secondly, why is it that this holds true only for arraylists and not the other collections?

Original source