使用 list.remove() 时的 Java ConcurrentModificationException [英] Java ConcurrentModificationException when using list.remove()
问题描述
我有一个名为 removeSup
的方法,它应该从补充列表中删除一个对象 Supplement
.这是该方法的代码:
I've got a method called removeSup
which is supposed to remove an object Supplement
from a list of supplements.
this is the code for the method:
private static void removeSup(Supplement supToRemove, List<Supplement> listToRemoveFrom) {
Iterator<Supplement> iterator = listToRemoveFrom.iterator();
while(iterator.hasNext()){
if(iterator.next().equals(supToRemove)){
iterator.remove();
}
}
}
有一个名为 magazine
的类,它定义了补充列表.
there is a class called magazine
which defines the list of supplements.
public class Magazine {
private List<Supplement> supList;
public List<Supplement> getSupList() {
return this.supList;
}
public void setSupList(List<Supplement> supList) {
this.supList = supList;
}
public Magazine(Double cost, String _name){
this.supList = new ArrayList<>();
this.weekCost = cost;
this.name = _name;
}
}
supplement
类具有以下构造函数
public Supplement(String _name, Double _price, String _magName ){
this.name=_name;
this.price=_price;
this.magName = _magName;
}
在主类 client
中有一个用户可以执行的搜索以删除某个补充
in the main class client
there is a search that the user can do to remove a certain Supplement
private static void searchSup(){
System.out.println("Search for Supplement");
String search = scanner.nextLine();
for (Supplement sup : magazine.getSupList()) {
if (!sup.getSupName().equalsIgnoreCase(search)) {
//do something
}
else{
removeSup(sup,magazine.getSupList());
}
}
}客户端类中的main方法如下:
} the main method in the client class is as follows:
private Magazine magazine;
public static void main(String[] args) {
magazine = new Magazine(3.0, "pop");
List<Supplement> startList = new ArrayList<>();
startList.add(new Supplement("Nat Geo", 3.0,"pop"));
startList.add(new Supplement("Discovery", 5.0,"pop"));
startList.add(new Supplement("Health", 6.3,"pop"));
startList.add(new Supplement("IT", 8.3,"pop"));
magazine.setSupList(startList);
searchSup();
}
当我运行这个程序并输入任何添加的补充时,我得到一个错误
When I run this program and type any of the added supplements, i get an error
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
at java.util.ArrayList$Itr.next(ArrayList.java:831)
at Client.searchSup(Client.java:131)
at Client.searchSup(Client.java:140)
at Client.main(Client.java:588)
这是我用来搜索的 for 循环给我一个错误吗?如果是这样,我将如何解决这个问题?
is it the for loop i am using to search giving me an error? if so how would i go about fixing this?
推荐答案
我发现我正在做的搜索不符合我想要做的,所以我创建了一个方法,它返回列表中补充的整数.
I figured out that the search i was doing was not working with what i wanted to do so i created a method which returns an integer of the Supplement in the list.
private static int indexOfSup(List<Supplement> supSearchList, String nameOfSup) {
for (Supplement sup : supSearchList) {
if (sup.getSupName().equalsIgnoreCase(nameOfSup)) {
return supSearchList.indexOf(sup);
}
}
return -1;
}
i 然后使用这个整数从列表中删除.一个简单的 List.Remove(index)
工作正常
i then use this integer to remove from the list.
a simple List.Remove(index)
worked fine
感谢大家的回复.
这篇关于使用 list.remove() 时的 Java ConcurrentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!