Java 如何在从ArrayList中删除对象时避免concurrentModificationException“;

Java 如何在从ArrayList中删除对象时避免concurrentModificationException“;,java,concurrentmodification,Java,Concurrentmodification,考虑以下代码 正如您所期望的,当在for each循环中删除水果时,deleteFroothByName方法抛出ConcurrentModificationException 在这种情况下,我如何避免这种情况 import java.util.ArrayList; public class Stringplay { public static void main(String[] args) { ArrayList<Fruit> fruites = new

考虑以下代码

正如您所期望的,当在for each循环中删除水果时,deleteFroothByName方法抛出ConcurrentModificationException

在这种情况下,我如何避免这种情况

import java.util.ArrayList;

public class Stringplay {
    public static void main(String[] args) {
        ArrayList<Fruit> fruites = new ArrayList<Fruit>();
        new Fruit(32, "apple", "red");
        new Fruit(64, "orange", "orange");
        new Fruit(12, "banana", "red");
        new Fruit(42, "grape", "purple");
        fruites.addAll(Fruit.fruits);
        Fruit.deleteFruitByName("apple");
        for (Fruit fruit : fruites) {
            System.out.println(fruit.getName());
        }
    }

}

public class Fruit {
    public int weight;
    public String name;
    public String type;
    public static ArrayList<Fruit> fruits = new ArrayList<Fruit>();

    public Fruit(int weight, String name, String type) {
        this.weight = weight;
        this.name = name;
        this.type = type;
        fruits.add(this);
    }

    public String getName() {
        return name;
    }

    public static void deleteFruitByName(String fruitName) {
        for (Fruit fruit : fruits) {
            if (fruit.getName().equals(fruitName)) {
                fruits.remove(fruit);
            }
        }

    }
}


import java.util.ArrayList;
公开课的弦乐{
公共静态void main(字符串[]args){
ArrayList水果=新的ArrayList();
新水果(32个,“苹果”、“红色”);
新水果(64种,“橙色”、“橙色”);
新水果(12种,“香蕉”、“红色”);
新水果(42种,“葡萄”、“紫色”);
水果。addAll(水果。水果);
水果。删除水果名称(“苹果”);
用于(水果:水果){
System.out.println(fruit.getName());
}
}
}
公共级水果{
公共权重;
公共字符串名称;
公共字符串类型;
公共静态ArrayList=新建ArrayList();
公共水果(整数重量、字符串名称、字符串类型){
重量=重量;
this.name=名称;
this.type=type;
水果。加上(这个);
}
公共字符串getName(){
返回名称;
}
公共静态void deleteFroothByName(字符串FroothName){
用于(水果:水果){
if(fruit.getName().equals(fruitName)){
水果。去掉(水果);
}
}
}
}

为了避免ConcurrentModificationException,您需要在此处使用迭代器

公共静态void deleteFroothByName(字符串FroothName){
迭代器it=fruits.Iterator();
while(it.hasNext()){
水果=it.next();
if(fruit.getName().equals(fruitName)){
it.remove();
}
}
}
来自java

这个类的迭代器和listIterator方法返回的迭代器是快速失效的:如果在迭代器创建后的任何时候,列表在结构上被修改,除了通过迭代器自己的remove或add方法,迭代器将抛出ConcurrentModificationException。因此,在面对并发修改时,迭代器会快速、干净地失败,而不是在将来的不确定时间冒着任意、不确定行为的风险

更新: 要在类Fluit中迭代集合,请使用以下代码

公共类主{
公共静态void main(字符串[]args){
新水果(32个,“苹果”、“红色”);
新水果(64种,“橙色”、“橙色”);
新水果(12种,“香蕉”、“红色”);
新水果(42种,“葡萄”、“紫色”);
水果。删除水果名称(“苹果”);
用于(水果:水果。水果){
System.out.println(fruit.getName());
}
}
}
以下操作应该有效(只需在此处编写,可能会出现编译错误):

公共静态void deleteFroothByName(字符串FroothName){ fruits.removif(fruit=>fruit.getName().equals(fruitName));
}

这样做是否也会从水果中移除对象?是的,它会移除。但是你的代码有一点错误。用这个水果。删除水果名(“苹果”);您可以从类Fluit中的静态集合中删除元素,但在main中使用for循环,您可以删除在main中初始化的其他集合。