Java 中断嵌套的 for 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4332345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:00:49  来源:igfitidea点击:

Break in nested for loops

javaloopsfor-loopbreaklabelled-break

提问by Upvote

Possible Duplicate:
How to Break from main/outer loop in a double/nested loop?

可能的重复:
如何在双/嵌套循环中从主/外循环中断?

I have the following situation:

我有以下情况:

      for(int i = 0; i < schiffe.length-1; i++){
            if(schiffe[i].schaden){
                schlepper.fliege(schiffe[i].x, 
                                 schiffe[i].y, 
                                 schiffe[i].z);
                schlepper.wirdAbgeschleppt = schiffe[i];
                for(int k = 0; k < stationen.length-1; k++){
                    if(stationen[k].reparatur == null){
                        schlepper.fliege(stationen[k].x,
                                         stationen[k].y,
                                         stationen[k].z);
                        break;
                    }
                }
            }
        }

I want to

我想要

schlepper.fliege(stationen[k].x,
                 stationen[k].y,
                 stationen[k].z);

be performed once and then break out of the inner loop and continue with the for(int i... loop. So I used a break in my code. But I am not really sure if this is right. Does the break cause a break for all loops or just for the second loop?

执行一次,然后跳出内循环并继续 for(int i... 循环。所以我在我的代码中使用了一个中断。但我不确定这是否正确。中断是否导致中断用于所有循环还是仅用于第二个循环?

采纳答案by Karl Knechtel

It breaks only the inner loop, and therefore does what you want. To break more than one level, in Java, you can use a "labelled break" like so:

它只中断内部循环,因此可以执行您想要的操作。要打破多个级别,在 Java 中,您可以使用“标记的中断”,如下所示:

schiffe_loop:
for(int i = 0; i < schiffe.length-1; i++){
    some_stuff();
    for(int k = 0; k < stationen.length-1; k++){
        if (something_really_bad_happened()) {
            break schiffe_loop;
        }
    }
}

But usually you will not need this.

但通常你不需要这个。

You may consider making a separate method for the inner loop anyway; it's something you can easily give a name to ("find_available_station" or something like that), and will probably need somewhere else.

无论如何,您可以考虑为内部循环创建一个单独的方法;这是您可以轻松命名的东西(“find_available_station”或类似的东西),并且可能需要其他地方。

Also, please be aware that your loops right now will miss the last element of each array. Think carefully about why you are using < instead of <=; it's exactly so that you use every value from 0 up to but not includingthe specified one.

另外,请注意您现在的循环将错过每个数组的最后一个元素。仔细想想你为什么使用 < 而不是 <=; 正是这样,您可以使用从 0 到但不包括指定值的每个值。

回答by Petro Semeniuk

break will cause break for closest loop only (second loop)

break 只会导致最近的循环中断(第二个循环)

回答by Carl Norum

breakonly breaks out of the innermost loop.

break只跳出最里面的循环。

回答by onemasse

Just the second one. You can use "goto" of course.

只是第二个。你当然可以使用“goto”。

回答by Jahan

Break statement terminates the closest enclosing loop or switch statement in which it appears in many languages but you can try it and be sure 100%.

Break 语句会终止它在许多语言中出现的最接近的封闭循环或 switch 语句,但您可以尝试并确保 100%。

回答by Dan D.

put the inner two loops in a function and use return to break out of both of them.

将内部的两个循环放在一个函数中,并使用 return 来打破它们。

回答by darioo

If you need more control, you can use a labeled break. More info here. As others have said, break will close the closest loop.

如果您需要更多控制,可以使用带标签的中断。更多信息在这里。正如其他人所说,break 将关闭最近的循环。

回答by codaddict

breakwill break the innermost loop, in your case 2nd loop.

break将打破最里面的循环,在你的情况下是第二个循环。

To make it break the outer loop you can use a labeled break as:

要使其中断外循环,您可以使用标记为中断:

OUTER:for(...) {
         for(...) {
            break OUTER;
         }
      }

回答by Jimit Tank

I Think You Can Do One More Thing..

我认为你可以再做一件事..

for(int k = 0; k < stationen.length-1; k++){ if(stationen[k].reparatur == null){ schlepper.fliege(stationen[k].x, stationen[k].y, stationen[k].z);

for(int k = 0; k < stationen.length-1; k++){ if(stationen[k].reparatur == null){ schlepper.fliege(stationen[k].x,stationen[k].y,stationen [k].z);

                   k =  stationen.length + 1 ;

                }
            }

回答by KristianB

What about using a boolean you call 'found' and use it like this:

使用您称为“找到”的布尔值并像这样使用它怎么样:

while(!found) {
    for(...) {
        //todo
    }
}