Is there an accepted best practice in Java for deleting a list element while iterating over the list?

后端 未结 5 1248
执笔经年
执笔经年 2021-01-21 07:15

I\'m finding conflicting advice over the best way to avoid a ConcurrentModificationException while doing this:

    List Apples = appleC         


        
5条回答
  •  感情败类
    2021-01-21 07:40

    List apples = appleCart.getApples();
    for (Iterator appleIterator = apples.iterator(); appleIterator.hasNext();)
    {
       Apple apple = appleIterator.next();
       if ( apple.isYucky() ) {
         appleIterator.remove();
       }
    }
    

提交回复
热议问题