如何打破 Java 中的嵌套循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/886955/
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
How do I break out of nested loops in Java?
提问by boutta
I've got a nested loop construct like this:
我有一个这样的嵌套循环结构:
for (Type type : types) {
for (Type t : types2) {
if (some condition) {
// Do something and break...
break; // Breaks out of the inner loop
}
}
}
Now how can I break out of both loops? I've looked at similar questions, but none concerns Java specifically. I couldn't apply these solutions because most used gotos.
现在我怎样才能跳出这两个循环?我看过类似的问题,但没有一个专门涉及 Java。我无法应用这些解决方案,因为大多数使用 goto。
I don't want to put the inner loop in a different method.
我不想将内部循环放在不同的方法中。
I don't want to rerun the loops. When breaking I'm finished with the execution of the loop block.
我不想重新运行循环。中断时,我完成了循环块的执行。
采纳答案by Jon Skeet
Like other answerers, I'd definitely preferto put the loops in a different method, at which point you can just return to stop iterating completely. This answer just shows how the requirements in the question can be met.
像其他回答者一样,我绝对更喜欢将循环放在不同的方法中,此时您可以返回以完全停止迭代。此答案仅显示如何满足问题中的要求。
You can use break
with a label for the outer loop. For example:
您可以将break
标签与外循环一起使用。例如:
public class Test {
public static void main(String[] args) {
outerloop:
for (int i=0; i < 5; i++) {
for (int j=0; j < 5; j++) {
if (i * j > 6) {
System.out.println("Breaking");
break outerloop;
}
System.out.println(i + " " + j);
}
}
System.out.println("Done");
}
}
This prints:
这打印:
0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
1 4
2 0
2 1
2 2
2 3
Breaking
Done
回答by simon622
You can use labels:
您可以使用标签:
label1:
for (int i = 0;;) {
for (int g = 0;;) {
break label1;
}
}
回答by Miguel Ping
You can use a temporary variable:
您可以使用临时变量:
boolean outerBreak = false;
for (Type type : types) {
if(outerBreak) break;
for (Type t : types2) {
if (some condition) {
// Do something and break...
outerBreak = true;
break; // Breaks out of the inner loop
}
}
}
Depending on your function, you can also exit/return from the inner loop:
根据您的功能,您还可以从内部循环退出/返回:
for (Type type : types) {
for (Type t : types2) {
if (some condition) {
// Do something and break...
return;
}
}
}
回答by Joey
You can use a named block around the loops:
您可以在循环周围使用命名块:
search: {
for (Type type : types) {
for (Type t : types2) {
if (some condition) {
// Do something and break...
break search;
}
}
}
}
回答by Fortega
Use a function:
使用一个函数:
public void doSomething(List<Type> types, List<Type> types2){
for(Type t1 : types){
for (Type t : types2) {
if (some condition) {
// Do something and return...
return;
}
}
}
}
回答by Zo72
Technically the correct answer is to label the outer loop. In practice if you want to exit at any point inside an inner loop then you would be better off externalizing the code into a method (a static method if needs be) and then call it.
从技术上讲,正确的答案是标记外循环。实际上,如果您想在内循环内的任何一点退出,那么最好将代码外部化为一个方法(如果需要,则为静态方法)然后调用它。
That would pay off for readability.
这将为可读性带来回报。
The code would become something like that:
代码会变成这样:
private static String search(...)
{
for (Type type : types) {
for (Type t : types2) {
if (some condition) {
// Do something and break...
return search;
}
}
}
return null;
}
Matching the example for the accepted answer:
匹配已接受答案的示例:
public class Test {
public static void main(String[] args) {
loop();
System.out.println("Done");
}
public static void loop() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i * j > 6) {
System.out.println("Breaking");
return;
}
System.out.println(i + " " + j);
}
}
}
}
回答by Elle Mundy
I never use labels. It seems like a bad practice to get into. Here's what I would do:
我从不使用标签。进入似乎是一种不好的做法。这是我会做的:
boolean finished = false;
for (int i = 0; i < 5 && !finished; i++) {
for (int j = 0; j < 5; j++) {
if (i * j > 6) {
finished = true;
break;
}
}
}
回答by Swifty McSwifterton
I needed to do a similar thing, but I chose not to use the enhanced for loop to do it.
我需要做类似的事情,但我选择不使用增强的 for 循环来做它。
int s = type.size();
for (int i = 0; i < s; i++) {
for (int j = 0; j < t.size(); j++) {
if (condition) {
// do stuff after which you want
// to completely break out of both loops
s = 0; // enables the _main_ loop to terminate
break;
}
}
}
回答by Mahesh Peri
You just use label for breaking inner loops
您只需使用标签来打破内部循环
public class Test {
public static void main(String[] args) {
outerloop:
for (int i=0; i < 5; i++) {
for (int j=0; j < 5; j++) {
if (i * j > 6) {
System.out.println("Breaking");
break outerloop;
}
System.out.println(i + " " + j);
}
}
System.out.println("Done");
}
}
回答by voidMainReturn
Even creating a flag for the outer loop and checking that after each execution of the inner loop can be the answer.
甚至为外循环创建一个标志并在每次执行内循环后检查它可以是答案。
Like this:
像这样:
for (Type type : types) {
boolean flag=false;
for (Type t : types2) {
if (some condition) {
// Do something and break...
flag=true;
break; // Breaks out of the inner loop
}
}
if(flag)
break;
}