Java , Removing object from ArrayList

前端 未结 4 449
独厮守ぢ
独厮守ぢ 2021-01-16 23:02

I have ClassA which has a static ArrayList of Objects

public static ArrayList meteorits = new ArrayList();

4条回答
  •  借酒劲吻你
    2021-01-16 23:46

    Basically, you need to use iterator to avoid such concurrent modification:

    List list = new ArrayList<>();
    for (Iterator iterator = list.iterator(); iterator.hasNext();) {
        String string = iterator.next();
        if (string.isEmpty()) {
            iterator.remove();
        }
    }
    

    For more details, please check out this post:

    Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop

提交回复
热议问题