-2

I have a nested for loop with about 8 loops nested within each other.

for (int n = 0; n < arrayAhu01.length(); n++) {
    JSONObject jsonAhu01 = arrayAhu01.getJSONObject(n); 
    cell4 = newCell(o1Arow, 3.25,jsonAhu01.get("TemperaturePVtag").toString());
    for (int m = 0; m < arrayAhu02.length(); m++) {
        JSONObject jsonAhu02 = arrayAhu02.getJSONObject(m);
        cell4 = newCell(o2Arow, 3.25, jsonAhu02.get("TemperaturePVtag").toString());

        for (int j = 0; j < arrayAhu05.length(); j++) {
            JSONObject jsonAhu05 = arrayAhu05.getJSONObject(j);
            cell4 = newCell(o3Arow, 3.25, jsonAhu05.get("HumiditySPtag").toString());

            for (int k = 0; k < arrayAhu06.length(); k++) {
                JSONObject jsonAhu06 = arrayAhu06.getJSONObject(k);
                cell4 = newCell(o4Arow, 3.25, jsonAhu05.get("HumiditySPtag").toString());
            }
        }
    }
}

I want this to work in a way that after each iteration completes for all the loops, the control should go from the innermost for loop to the outermost loop. This is required to fill in rows in a PDF table in java one by one. (Please ignore the codes regarding cells as its related to PDF table) but in my case the iteration for the innermost loop is happening till it is complete and then finally moves to outermost loop and causes memory exception.

mnestorov
  • 4,116
  • 2
  • 14
  • 24
Krishna
  • 21
  • 1
  • 9

3 Answers3

0

Use labels in java It will make your code jump from innermost loop to the outermost loop after every iteration. Some what Like this.

//Label used here
outer:     
for (int n = 0; n < arrayAhu01.length(); n++) {
        JSONObject jsonAhu01 = arrayAhu01.getJSONObject(n); 
        cell4 = newCell(o1Arow, 3.25,jsonAhu01.get("TemperaturePVtag").toString());
        for (int m = 0; m < arrayAhu02.length(); m++) {
            JSONObject jsonAhu02 = arrayAhu02.getJSONObject(m);
            cell4 = newCell(o2Arow, 3.25, jsonAhu02.get("TemperaturePVtag").toString());

            for (int j = 0; j < arrayAhu05.length(); j++) {
                JSONObject jsonAhu05 = arrayAhu05.getJSONObject(j);
                cell4 = newCell(o3Arow, 3.25, jsonAhu05.get("HumiditySPtag").toString());

                for (int k = 0; k < arrayAhu06.length(); k++) {
                    JSONObject jsonAhu06 = arrayAhu06.getJSONObject(k);
                    cell4 = newCell(o4Arow, 3.25, 
                    jsonAhu05.get("HumiditySPtag").toString());
                    // terminate loop and go back to label name
                    break outer;
                }
            }
        }
    }
  • Thanks. Just a question. should it be "break outer" or "continue outer" in the innermost loop? I am aware that continue will jump to next iteration, so wonderding if continue outer will go to outer loop's 2nd iteration or break outer will work in this case. – Krishna Jul 18 '19 at 06:53
0

Well you can handle using conditions, Just need to take flag as per number of your loops and manage them accordingly,

Here I have written sample method , Once you run in debug mode you will able to understand it , Please write me into comment if you have any query.

Using this approach you can jump to any loop you want by just setting the flag.

        public static void main(String[] args) {
                boolean fisrtLoopflag = true;
                boolean secondLoopflag = true;
                boolean thirdLoopflag = true;

                for (int m = 0; m < 10; m++) {
                    System.out.println("m" + m);
                    if (fisrtLoopflag) {
                        for (int j = 0; j < 10; j++) {
                            System.out.println("j" + j);
                            if (secondLoopflag) {
                                for (int k = 0; k < 10; k++) {

                                    if (thirdLoopflag) {

                                        System.out.println("k" + k);
                                        if (k == 1) {
                                            secondLoopflag = false;
                                            thirdLoopflag = false;
                                            break;
                                        }
                                    } else {
                                        break;
                                    }
                                }
                            } else {
                                break;
                            }
                        }
                    } else {
                        break;
                    }
                }

            }
Mak
  • 1,068
  • 8
  • 19
0

You can use labeling in JAVA

outer :
for (int n = 0; n < arrayAhu01.length(); n++) 
    {
    JSONObject jsonAhu01 = arrayAhu01.getJSONObject(n); 
    cell4 = newCell(o1Arow, 3.25,jsonAhu01.get("TemperaturePVtag").toString());
           for (int m = 0; m < arrayAhu02.length(); m++) 
           {
              JSONObject jsonAhu02 = arrayAhu02.getJSONObject(m);
              cell4 = newCell(o2Arow, 3.25, jsonAhu02.get("TemperaturePVtag").toString());

                 for (int j = 0; j < arrayAhu05.length(); j++) 
                    {
                        JSONObject jsonAhu05 = arrayAhu05.getJSONObject(j);
                        cell4 = newCell(o3Arow, 3.25, jsonAhu05.get("HumiditySPtag").toString());

                   for (int k = 0; k < arrayAhu06.length(); k++) 
                                {
                                    JSONObject jsonAhu06 = arrayAhu06.getJSONObject(k);
                                    cell4 = newCell(o4Arow, 3.25, jsonAhu05.get("HumiditySPtag").toString());
                                    continue outer;
                                }
                    }
           }
   }

Here assign a label to outermost loop and when we reach innermost loop,till each loop complete iteration then we pass control back to outermost loop

  • Thank you.. So I can just name the outermost for loop and use continue for that, in the innermost loop? Would break outer work differently in this case? – Krishna Jul 18 '19 at 06:53
  • Is this solution is suitable for your logic or you need something else?? – MohammadTausif Shaikh Jul 18 '19 at 08:39
  • Sorry for my belated response. But I got it to work atlast. Unfortunately, using the above approach didnt suit my logic as the increment for the inner loops kept resetting after each time the continue was called. I changed the forloops to while loop and declared the local iteration variables outside the while loops and used the same labelling+continue logic. It works fine now. Thank you for your help :) – Krishna Jul 25 '19 at 19:20