How do I break out of nested loops in Java?

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

    I never use labels. It seems like a bad practice to get into. Here's what I would do:

    boolean finished = false;
    for (int i = 0; i < 5 && !finished; i++) {
        for (int j = 0; j < 5; j++) {
            if (i * j > 6) {
                finished = true;
                break;
            }
        }
    }
    

提交回复
热议问题