control structure - Accessing index when iterating over java array for loop -


this perhaps random , nitpicky question, i've been doing lot of work array manipulation in java recently, , wondering whether, when using following loop:

for (object obj : somearrayorcollection) {     //do } 

if there's way access kind of ordinal without doing (declaring variable , adding 1 every time loop runs) or 1 of coworkers (using

list.indexof(int i)  

method)? feel both of these add rather lot of overhead. know use standard

for (declaration;condition;change) { } 

loop, it's more convenient in context of project use modified loop. so, question is, there way access index of object you're working without resorting more memory-intensive operation have above?

thanks!

no, for-each loops not keep track of index on because not every iterable indexable. sets, example, have no concept of order, makes no sense "the loop on element nth index". if want keep track of iteration loop on, need use counter suggested.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -