加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 替代’continue’关键字[已关闭]

发布时间:2020-12-14 05:18:48 所属栏目:Java 来源:网络整理
导读:我正在浏览有关继续关键字的问题,以更好地了解它,我偶然发现这条线 in this answer These can be maintenance timebombs because there is no immediate link between the “continue”/”break” and the loop it is continuing/breaking other than contex
我正在浏览有关继续关键字的问题,以更好地了解它,我偶然发现这条线 in this answer

These can be maintenance timebombs because there is no immediate link between the “continue”/”break” and the loop it is continuing/breaking other than context;

我有这个循环:

for(Object obj : myArrayList){
  if(myArrayList.contains(someParticularData)){
       continue;
    }
   //do something
}

现在,我的问题是 – 是否可以以上述方式使用继续或者是否有任何问题?如果是,我可以采取什么替代方法?任何一种指导都会有所帮助.谢谢.

更新:在这种特殊情况下,我的目标是迭代一个Collection(在这种情况下是ArrayList),并检查它是否包含一些特定的数据,如果它是true,则跳过该迭代.
我被指出,myArrayList.contains(someParticularData)是一次性操作,在循环之外执行该检查会更好,这是我正在寻找的.此外,我了解到,如果我可以使用继续基于某些条件if(someConditon),我可以很好地避免使用if(!someCondition).

解决方法

for(Object obj : myArrayList){
    if(someCondition){
       continue;
    }
    //do something 
}

可以替换为:

for(Object obj : myArrayList){
    if(!someCondition){
       //do something 
    }
}

嗯,只要你没有很多(如2-3继续/中断/返回),维护将会很好.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读