You have to use your own counter like this one
int counter = 0;
ArrayList<User> userList;
for(User user:userList){
....
counter++;
}
This is because java for each loop is based on iterator which is not based on index based iteration. Actually foreach loop internally uses an iterator. so it doesn't have any index. It moves automatically over the items of a collection or list, grabbing a reference to each one as you need it.
See this image
In this way iterator traverse each element or object without indexes. So as the for loop.
or you can use
User user = null;
for (int counter = 0; counter < userList.size(); counter++) {
user = userList.get(counter);
}