Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中打破嵌套for循环_Java_Loops_For Loop_While Loop_Break - Fatal编程技术网

在Java中打破嵌套for循环

在Java中打破嵌套for循环,java,loops,for-loop,while-loop,break,Java,Loops,For Loop,While Loop,Break,可能重复: 如何使用break和/或continue语句返回while循环的第一行(例如,如伪代码中所示)的第1、2和3点 假设我有一个让人想起以下场景: while(condition) { // want to return to this point for (Integer x : xs) { // point 1 for (Integer y : ys) { // point 2 ...

可能重复:

如何使用break和/或continue语句返回while循环的第一行(例如,如伪代码中所示)的第1、2和3点

假设我有一个让人想起以下场景:

while(condition) {
    // want to return to this point
    for (Integer x : xs) {
        // point 1
        for (Integer y : ys) {
            // point 2
            ...
        }
        ...
    }
    for (Integer a : as) {
        for (Integer b : bs) {
            // point 3
            ...
        }
        ...
    }
}

是的,您可以使用带有标签的
break
语句:

SOME_LABEL:
   // ... some code here
   break SOME_LABEL;
少用,这是一种打破多个嵌套循环的干净方法。

使用以下标签:

outer:
while(condition) {
// want to return to this point
for (Integer x : xs) {
    // point 1
    for (Integer y : ys) {
        // point 2
        ...
    }
    ...
}
for (Integer a : as) {
    for (Integer b : bs) {
        // point 3
        ...
    }
    ...
}
}

然后您可以使用
断开外部以退出while循环。这也适用于嵌套for循环,但我尽量不过度使用标签

正如@Peter所指出的,使用
continue outer如果您希望提前完成当前外部迭代并继续下一个迭代,而不是逃避while循环。

您可以这样做:

here:
while(condition) {
   for (Integer x : xs) {
        // point 1
        for (Integer y : ys) {
            break here;
        }
    }
带或不带标签的break语句:

没有标签的break语句尝试将控制转移到 最里面的封闭开关,while、do或for语句 立即封闭方法或初始值设定项;这句话是 调用中断目标,然后立即正常完成

带有标签标识符的break语句尝试传输控制 附标签的语句(§14.7)具有相同的 标识符作为其标签;此语句称为中断 目标,然后立即正常完成。在这种情况下,中断 目标不必是switch、while、do或for语句

使用语法创建循环

LABEL:LoopType
之后,您可以使用Break语句使用
Break LABEL退出某个循环

OUT: while(somecond){
IN:for(...) {
   INNER: for(...){
break IN;// will get you outta IN and INNER for loop
}
 }
}

}

您可以像这里发布的stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java处理相同的问题。

谢谢!我不知道你可以用不同类型的循环标签。这真的很有帮助。+1标签通常是大写的。我假设你想要
继续中断也将退出该循环。@Froskoy您可以中断任何代码块,它不一定是一个循环。@PeterLawrey提出了一个很好的观点,遵循用于常量的命名约定“是否存在某种中断语句”我相信,只有一种休息exist@coders:谢谢-为了清晰起见,我进行了编辑。@OldCurmudgeon:我非常不同意这篇文章是重复的。事实上,涉及两种不同类型的循环,并且有多个连续的内部循环,这使得这个问题与站点上的其他问题非常不同。在这种情况下使用break和continue有什么区别?continue返回到循环的开始,break退出循环,实际上并没有回答我的问题,或者至少当我在阅读其他答案之前第一次看到它时,我认为它没有,因为混合的for和while循环,并且在微妙的不同点爆发。
loop:
while(condition) {
// want to return to this point
for (Integer x : xs) {
    continue loop:
    for (Integer y : ys) {
        continue loop:
        ...
    }
    ...
}
for (Integer a : as) {
    for (Integer b : bs) {
        continue loop:
        ...
    }
    ...
}