No, the for-each loop does not track the index. You can use a regular indexed loop or do something like this:
int idx = 0; for (Object o : list) { ... idx++; }
This is dangerous because break / continue will make idx not synchronized, so use it infrequently and only when the body is simple and only a few lines away.
If the elements are different, List.indexOf will also work, albeit in O(N) , and at this point you may consider a Set (unordered, but guaranteed report).
It should also be said that sometimes using listIterator() also facilitates the need for an explicit index during iteration.
A ListIterator supports add , Set and remove operations.
This is another clear advantage. List has redundant arrays as iteration mechanism progresses.
source share