For a considerable number of cases the enhanced for loop is useless:
when you need the index of the element. I prefer
for (int i = 0; i < coll.size(); i++) {
// do somehing with i
// do something with coll.get(i);
}
rather than
int index = 0;
for (Clazz elem : coll) {
// do something with index
// do something with elem
}
In the first approach, the scope of i is just the for block, while in the second case, i is also accessible in the parent block. If we could somehow get a counter variable in the enhanced for loop maybe I would have changed my mind. |