Generic for-loop: Is there a way to get iteration information?

java, loops

Solution

Briefly, no. You have to use the indexing method to do that.

Problem

I am used to implement loops with generics like that: ``` for (final Dog lDog : lAllDog) { ... } ``` Unfortunality for another business case I need the current count of the iteration. I know I can solve this by coding somthing like that: ``` for (int i = 0 ; i < lAllDog.length(); i++) { System.out.println(i); } ``` or ``` int i = 0; for (final Dog lDog : lAllDog) { ... i++; } ``` but is there a way to get the current count of iteration with my first code example without declaring a new `int` or change the whole loop header? Thx a lot

Original source