简体   繁体   English

在 Java for 循环中每次迭代后,如何从最内层循环跳到最外层循环?

[英]How do I jump from the innermost loop to the outermost loop after every iteration in Java for loop?

I have a nested for loop with about 8 loops nested within each other.我有一个嵌套的 for 循环,其中包含大约 8 个相互嵌套的循环。

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.我希望它以这样一种方式工作:在所有循环的每次迭代完成后,控件应该从最里面的 for 循环到最外面的循环。 This is required to fill in rows in a PDF table in java one by one.这是在java中一一填充PDF表格中的行所必需的。 (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. (请忽略有关单元格的代码,因为它与 PDF 表相关)但在我的情况下,最内层循环的迭代正在发生,直到它完成,然后最终移动到最外层循环并导致内存异常。

Use labels in java It will make your code jump from innermost loop to the outermost loop after every iteration.在java中使用标签它会让你的代码在每次迭代后从最内层循环跳到最外层循环。 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;
                }
            }
        }
    }

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;
                    }
                }

            }

You can use labeling in JAVA您可以在 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这里为最外层循环分配一个标签,当我们到达最内层循环时,直到每个循环完成迭代然后我们将控制权传回最外层循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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