Can I use the same size for each of the two arrays

I have two arrays:

name[] and roll[]

Is there a way to move both arrays one at a time for each loop. The size of both arrays remains unchanged.

I know that with the help of two separate loops we can move and insert into one and that it is not very important, but I want something like this:

for(String n:name,int r:roll){
  //blah blah
}

I'm sorry, thanks ..... Ankur

+5
source share
4 answers

No. You will have to use old fashioned

for(int index = 0; index < name.length; index++) {
  //blah blah with name[index] and roll[index]
}
+7
source

No. You cannot cross two identical arrays with one cycle for-each.

If you want to iterate both arrays in one loop, you will have to use the traditional java loop for

+4
source
for(int i=0,len=name.length; i<len; i++) {
     String n = name[i];
     int r = roll[i];
}
+2

The loop for...eachdoes not display the index (intentionally, in fact, it does not even have one). You can use your own index if you really lean on it, but you better use the good old loop with the index.

Here's how you could do it with your own index:

{
    int index = 0;
    for(String name : names) {
        // roll[index];
        ++index
    }
}

Also see this answer.

+2
source

All Articles