How do I break out of nested loops in Java?

前端 未结 30 2769
梦毁少年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:22

    Rather unusual approach but in terms of code length (not performance) this is the easiest thing you could do:

    for(int i = 0; i++; i < j) {
        if(wanna exit) {
            i = i + j; // if more nested, also add the 
                       // maximum value for the other loops
        }
    }
    

提交回复
热议问题