Java – replace ‘continue’ keyword [closed]

I was browsing the question about the continue keyword to better understand it. I came across this line in this answer

I have this cycle:

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

Now, my question is – can I continue using it in the above way, or are there any questions? If so, what alternatives can I take? Any kind of guidance will help thank you.

Update: in this special case, my goal is to iterate over a collection (ArrayList in this case) and check whether it contains some specific data. If it is true, skip the iteration I was pointed out that myarraylist Contains (some granular data) is a one-time operation. It would be better to perform this check outside the loop, which is what I am looking for In addition, I learned that if I can continue to use if (somecondition) based on some conditions, I can well avoid using if (! Somecondition)

Solution

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

Can be replaced by:

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

Well, as long as you don't have a lot (such as 2-3 continue / interrupt / return), maintenance will be good

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>