What, if any, is the performance difference between the following two loops?
for (Object o: objectArrayList) {
o.DoSomething();
}
and <
Well, performance impact is mostly insignificant, but isn't zero. If you look at JavaDoc of RandomAccess
interface:
As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class, this loop:
for (int i=0, n=list.size(); i < n; i++) list.get(i);
runs faster than this loop:
for (Iterator i=list.iterator(); i.hasNext();) i.next();
And for-each loop is using version with iterator, so for ArrayList
for example, for-each loop isn't fastest.