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

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

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

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

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


当前回答

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

其他回答

您可以使用标签:

label1: 
for (int i = 0;;) {
    for (int g = 0;;) {
      break label1;
    }
}

for(int j=0;j<5;j++)//内部循环应替换为对于(int j=0;j<5&&!exitloops;j++)。

这里,在这种情况下,如果条件为True,则应退出完整的嵌套循环。但如果我们只对上循环使用exitloops

 for (int i = 0; i < 5 && !exitloops; i++) //upper loop

然后,内部循环将继续,因为并没有额外的标志通知此内部循环退出。

示例:如果i=3和j=2,则条件为假。但在下一次内循环迭代中,j=3,则条件(i*j)变为9,这是真的,但内循环将继续,直到j变为5。

因此,它也必须对内部循环使用exitloops。

boolean exitloops = false;
for (int i = 0; i < 5 && !exitloops; i++) { //here should exitloops as a Conditional Statement to get out from the loops if exitloops become true. 
    for (int j = 0; j < 5 && !exitloops; j++) { //here should also use exitloops as a Conditional Statement. 
        if (i * j > 6) {
            exitloops = true;
            System.out.println("Inner loop still Continues For i * j is => "+i*j);
            break;
        }
        System.out.println(i*j);
    }
}

你只需要使用标签来打破内部循环

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

我觉得使用标签使代码看起来非常像goto语句。这只是一个想法。

相反,在内部for循环中抛出一个异常,并用try-catch块封装两个for循环。

类似于

try {
  // ...
  for(Object outerForLoop : objectsOuter) {
     // ...
     for (Object innerForLoop : objectsInner) {
        // ...
        if (isConditionTrue)
             throw new WrappedException("With some useful message. Probably some logging as well.");
     }
  }
  catch (WrappedException) {
        // Do something awesome or just don't do anything to swallow the exception.
  }

只是一个想法。我更喜欢这段代码,因为当它在生产环境中运行时,它给了我更好的逻辑性(就像这是一个词)。

和其他回答者一样,我肯定更喜欢将循环放在不同的方法中,此时您可以返回以完全停止迭代。这个答案正好说明了如何满足问题中的要求。

可以将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");
    }
}

这将打印:

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