简体   繁体   中英

Nested for loop inside a while loop - Java

I was working on a Java web application, and had the following requirement with respect to looping in my HTML table.

I've a nested for loop inside a while loop(both execute the same # of times, for ex. say 3).

My code looks something like this:

<table>
<thead>...</thead>
<tbody>

    if (patcases != null && patcases.size() > 0) {
                Iterator itr1 = patcases.iterator();
                while (itr1.hasNext()) {
                    ..some code here..

                    System.out.println("DA Email from webpage..."+da.getEmail());
                       int rCount = 0;  
       <tr>                
                       for(int i=0;i<passedValues.length; i++){

                         ...some code here..
       </tr>

                       System.out.println("Printed row..." +rCount);
                rCount ++;
} /*closing of for loop */
}/*closing of while loop */
}/* closing of if loop */
</tbody>
</table>

Now, with this type of looping structure, I get the following on my console:

DA Email from webpage...abc@abc.com
Printed row...0
Printed row...1
Printed row...2
DA Email from webpage...xyz@xyz.com
Printed row...0
Printed row...1
Printed row...2
DA Email from webpage...123@123.com
Printed row...0
Printed row...1
Printed row...2

But the type of output I wanted was, something as follows:
DA Email from webpage...abc@abc.com
Printed row...0
DA Email from webpage...xyz@xyz.com
Printed row...1
DA Email from webpage...123@123.com
Printed row...2

How would I go about doing this?
Any help would be greatly appreciated.

It looks like you want parallel iteration .

Simply do something like this:

Iterator<?> iter1 = ...;
Iterator<?> iter2 = ...;             // or: int index = 0;

while (iter1.hasNext() &&
           iter2.hasNext()) {        // or: index < MAX

   Object item1 = iter1.next();
   Object item2 = iter2.next();      // or: index++;

   doSomething(item1, item2);        // or: doSomething(item1, index);

}

// perhaps additional handling if one ran out before the other

Note that if at all possible, so you should use parameterized types instead of raw types ( Effective Java 2nd Edition, Item 23: Don't use raw types in new code ).

It seems to me that you don't want a nested for loop at all. You just want a counter which gets incremented in the while loop:

if (patcases != null && patcases.size() > 0) {
     Iterator itr1 = patcases.iterator();
     int index = 0;
     while (itr1.hasNext()) {
         ..some code here..
         System.out.println("DA Email from webpage..."+da.getEmail());
         if (index < passedValues.length) {
             System.out.println("Printed row..." + index);
         } else {
             // Hmm, didn't expect this...
             // (Throw exception or whatever)
         }
         index++;
     }
     if (index != passedValues.length) {
         // Hmm, didn't expect this...
         // (Throw exception or whatever)
     }
 }

"Nested for loops" doesn't mean "interlaced for loops". For example, saying:

for (i = 0; i < 3; i++) {
    print("i: " + i);

    for (j = 0; j < 3; j++)
        print("\tj: " + j);
}

prints the following:

i: 0
    j: 0
    j: 1
    j: 2
i: 1
    j: 0
    j: 1
    j: 2
i: 2
    j: 0
    j: 1
    j: 2

What you seem to want is:

i: 0
    j: 0
i: 1
    j: 1
i: 2
    j: 2

Instead of nesting for loops, you would just use a separate counter in the same loop:

j = 0;

for (i = 0; i < 3; i++) {
    print("i: " + i);

    print("\tj: " + j);
    j++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM