Aimad MAJDOU Aimad MAJDOU - 5 months ago 8
Java Question

Get the current index of a for each loop iterating an ArrayList

I'm iterating an

ArrayList
using the for each loop, but I don't know how to get the current index where the loop is.

I did Google it, but I couldn't find anything helpful.

Please, if someone could tell me how to get the current index, I'll be grateful.

Answer

Just use a traditional for loop:

for (int i = 0; i < yourArrayList.size(); i ++) {
    // i is the index
    // yourArrayList.get(i) is the element
}