This is perhaps a random and nitpicky question, but I've been doing a lot of work with array manipulation in Java recently, and I was wondering whether, when using the following loop:
for (Object obj : SomeArrayOrCollection) {
//do something
}
if there's any way to access some kind of ordinal without doing what I usually do (declaring an extra variable and adding one to it every time the loop runs) or what one of my coworkers does (using the
List.indexOf(int i)
method)? I feel like both of these add rather a lot of overhead. I know that I could just use a standard
for (declaration;condition;change) { }
loop, but it's significantly more convenient in the context of this project to use the modified for loop. So, the question is, is there any way to access the index of the object you're working with without resorting to a more memory-intensive operation like I have above?
Thanks!