How do I break out of nested loops in Java?

前端 未结 30 2800
梦毁少年i
梦毁少年i 2020-11-21 11:51

I\'ve got a nested loop construct like this:

for (Type type : types) {
    for (Type t : types2) {
         if (some condition) {
             // Do somethin         


        
30条回答
  •  终归单人心
    2020-11-21 12:03

    Like @1800 INFORMATION suggestion, use the condition that breaks the inner loop as a condition on the outer loop:

    boolean hasAccess = false;
    for (int i = 0; i < x && hasAccess == false; i++){
        for (int j = 0; j < y; j++){
            if (condition == true){
                hasAccess = true;
                break;
            }
        }
    }
    

提交回复
热议问题