本文介绍了如何打破多个foreach循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个foreach循环遍历集合,并根据条件做一些事情。这里是我现在写的代码:

$ $ $ $ $ $ $ $布尔值breakFlag = false;
字符串valueFromObj2 = null;
字符串valueFromObj4 = null; (Object1 object1:objects){
for(Object2 object2:object1){
//我从object2得到一些值
valueFromObj2 = object2.getSomeValue();
for(Object3 object3:object2){
for(Object4 object4:object3){
//最后我从Object4中得到一些值。
valueFromObj4 = object4.getSomeValue();
//比较valueFromObj2来决定是否打破所有的foreach循环
breakFlag = compareTwoVariable(valueFromObj2,valueFromObj4);
if(breakFlag){break;}
} //第四个循环在这里结束
if(breakFlag){break;}
} //第三个循环在这里结束
if(breakFlag){break;}
} //第二个循环在这里结束
if(breakFlag){break;}
} //第一个循环在这里结束
$ b

主要对象(代码中的对象)来自第三方供应商SDK,因此我无法更改该部分的任何内容。我想问问stackoverflow社区,如果有更好的方法来打破所有的四个foreach循环。或者,如果有任何其他方式来重构此代码,使其更具可读性和可维护性。谢谢。

解决方案

在最外面的循环中使用一个标签,并在 break 语句,当你想跳出所有的循环。在下面的例子中,我修改了你的代码来使用标签 OUTERMOST

  String valueFromObj2 = null; 
字符串valueFromObj4 = null;
OUTERMOST:for(Object1 object1:objects){
for(Object2 object2:object1){
//我从object2得到一些值
valueFromObj2 = object2.getSomeValue();
for(Object3 object3:object2){
for(Object4 object4:object3){
//最后我从Object4中得到一些值。
valueFromObj4 = object4.getSomeValue();
//比较valueFromObj2来决定是否打破所有的foreach循环
if(compareTwoVariable(valueFromObj2,valueFromObj4)){
break OUTERMOST;
}
} //第四个循环在这里结束
} //第三个循环在这里结束
} //第二个循环在这里结束
} //第一个循环在这里结束


I have four foreach loops that iterate through the collections and based on a condition do something. Here is the code that I am writing now:

boolean breakFlag = false;
String valueFromObj2 = null;
String valueFromObj4 = null;
for(Object1 object1: objects){
  for(Object2 object2: object1){
    //I get some value from object2
    valueFromObj2 = object2.getSomeValue();
    for(Object3 object3 : object2){
      for(Object4 object4: object3){
       //Finally I get some value from Object4.
       valueFromObj4 = object4.getSomeValue();
       //Compare with valueFromObj2 to decide either to break all the foreach loop
       breakFlag = compareTwoVariable(valueFromObj2, valueFromObj4 );
       if(breakFlag){break;}
      }//fourth loop ends here
      if(breakFlag){break;}
    }//third loop ends here
    if(breakFlag){break;}
  }//second loop ends here
  if(breakFlag){break;}
}//first loop ends here

The main object (objects in the code) comes from a third party provider SDK, so I cannot change anything on that portion. I want to ask the stackoverflow community if there is a better approach to break all the four foreach loop. Or if there is any other way to refactor this code to make it more readable and maintainable. Thanks.

解决方案

Use a label on the outermost loop, and include this label in the break statement when you want to jump out of all the loops. In the example below, I've modified your code to use the label OUTERMOST:

String valueFromObj2 = null;
String valueFromObj4 = null;
OUTERMOST: for(Object1 object1: objects){
  for(Object2 object2: object1){
    //I get some value from object2
    valueFromObj2 = object2.getSomeValue();
    for(Object3 object3 : object2){
      for(Object4 object4: object3){
        //Finally I get some value from Object4.
        valueFromObj4 = object4.getSomeValue();
        //Compare with valueFromObj2 to decide either to break all the foreach loop
        if( compareTwoVariable(valueFromObj2, valueFromObj4 )) {
          break OUTERMOST;
        }
      }//fourth loop ends here
    }//third loop ends here
  }//second loop ends here
}//first loop ends here

这篇关于如何打破多个foreach循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:55