请考虑以下代码。如您所料,它将引发ConcurrentModificationException。在这种情况下如何避免这种情况?................................................... ...................................................
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());
}
}
}
import java.util.ArrayList;
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 int getWeight() {
return weight;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public static void deleteFruitByName(String fruitName) {
for (Fruit fruit : fruits) {
if (fruit.getName().equals(fruitName)) {
fruits.remove(fruit);
}
}
}
}
为了避免ConcurrentModificationException,您需要在此处使用Iterator。
public static void deleteFruitByName(String fruitName) {
Iterator<Fruit> it = fruits.iterator();
while (it.hasNext()) {
Fruit fruit = it.next();
if (fruit.getName().equals(fruitName)) {
it.remove();
}
}
}
来自Java doc
由此类的迭代器和listIterator方法返回的迭代器是快速失败的:如果在创建迭代器后的任何时候以任何方式对列表进行结构修改,除非通过迭代器自己的remove或add方法,否则迭代器都会抛出一个ConcurrentModificationException。因此,面对并发修改,迭代器会快速干净地失败,而不会在未来的不确定时间内冒任意,不确定的行为的风险。
更新:要在Fluit类中迭代集合,请使用此代码
public class Main {
public static void main(String[] args) {
new Fruit(32, "apple", "red");
new Fruit(64, "orange", "orange");
new Fruit(12, "banana", "red");
new Fruit(42, "grape", "purple");
Fruit.deleteFruitByName("apple");
for (Fruit fruit : Fruit.fruits) {
System.out.println(fruit.getName());
}
}
}