我有一个这样的嵌套循环构造:

for (Type type : types) {
    for (Type t : types2) {
         if (some condition) {
             // Do something and break...
             break; // Breaks out of the inner loop
         }
    }
}

现在我怎样才能打破这两个循环?我已经研究过类似的问题,但没有一个与Java相关。我无法应用这些解决方案,因为大多数人都使用goto。

我不想将内部循环放在不同的方法中。

我不想返回循环。当中断时,我完成了循环块的执行。


当前回答

从技术上讲,正确的答案是标记外循环。在实践中,如果您想在内部循环中的任何点退出,那么最好将代码外部化为方法(如果需要的话是静态方法),然后调用它。

这将有助于提高可读性。

代码会变成这样:

private static String search(...) 
{
    for (Type type : types) {
        for (Type t : types2) {
            if (some condition) {
                // Do something and break...
                return search;
            }
        }
    }
    return null; 
}

匹配接受答案的示例:

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

其他回答

boolean condition = false;
for (Type type : types) {
    for (int i = 0; i < otherTypes.size && !condition; i ++) {
        condition = true; // If your condition is satisfied
    }
}

将条件用作完成处理时的标志。然后,内部循环仅在条件未满足时继续。无论哪种方式,外环都会继续发出嗡嗡声。

您可以在不使用任何标签:和标志的情况下断开所有循环。

这只是一个棘手的解决方案。

这里条件1是用于从循环K和J中断的条件。条件2是用于从循环K、J和I中分离的条件。

例如:

public class BreakTesting {
    public static void main(String[] args) {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                for (int k = 0; k < 9; k++) {
                    if (condition1) {
                        System.out.println("Breaking from Loop K and J");
                        k = 9;
                        j = 9;
                    }
                    if (condition2) {
                        System.out.println("Breaking from Loop K, J and I");
                        k = 9;
                        j = 9;
                        i = 9;
                    }
                }
            }
        }
        System.out.println("End of I , J , K");
    }
}

当您需要退出多个循环时,单独使用“break”关键字不是合适的方法。您可以退出立即循环无论语句周围有多少个循环。你可以在标签上使用“break”!这里我使用了标签“abc”您可以在Java的任何函数中编写如下代码

这段代码显示了如何退出最外层的循环

 abc: 
    for (int i = 0; i < 10; i++) { 
        for (int j = 0; j < 10; j++) { 
           for (int k = 0; k < 10; k++) { 
              if (k == 1){
                 break abc;
              } 
        } 
    } 
}

您还可以使用break语句退出嵌套循环中的任何循环。

    for (int i = 0; i < 10; i++) { 
       abc:for (int j = 0; j < 10; j++) { 
           for (int k = 0; k < 10; k++) { 
              if (k == 1){
                 break abc;
              } 
        } 
    } 
}

下面的代码显示了从最内部循环退出的示例。在其他工作中,在执行以下代码后,您处于“k”变量循环的外部,仍然处于“j”和“i”变量循环内部。

    for (int i = 0; i < 10; i++) { 
        for (int j = 0; j < 10; j++) { 
           for (int k = 0; k < 10; k++) { 
              if (k == 1){
                 break;
              } 
        } 
    } 
}

Java不像C++那样具有goto特性。但在Java中,goto仍然是一个保留关键字。他们可能会在未来实施它。对于您的问题,答案是Java中有一种叫做标签的东西,您可以对其应用continue和break语句。查找以下代码:

public static void main(String ...args) {
    outerLoop: for(int i=0;i<10;i++) {
    for(int j=10;j>0;j--) {
        System.out.println(i+" "+j);
        if(i==j) {
            System.out.println("Condition Fulfilled");
            break outerLoop;
        }
    }
    }
    System.out.println("Got out of the outer loop");
}

如果它在某个函数中,为什么不直接返回它:

for (Type type : types) {
    for (Type t : types2) {
         if (some condition) {
            return value;
         }
    }
}