【发布时间】:2014-05-14 14:11:06
【问题描述】:
我有一个名为removeSup 的方法,它应该从补充列表中删除对象Supplement。
这是方法的代码:
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 的类定义了补充列表。
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 中有一个搜索,用户可以通过它来删除某个补充
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方法如下:
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();
}
当我运行这个程序并输入任何添加的补充时,我得到一个错误
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 循环给了我一个错误吗?如果是这样,我将如何解决这个问题?
【问题讨论】:
-
您是否阅读了有关 ConcurrentModificationException 的 javadoc?另外,您是否搜索过类似的问题?
-
不要使用新的迭代器在
removeSup中再次遍历列表,而是使用searchSup中的显式迭代器进行迭代,并在searchSup中使用该迭代器的remove。 -
@user2357112 有最好的答案 IMO
-
@user2357112 我尝试这样做并得到同样的错误。 `else{ 杂志.getSupList().remove(sup); } 喜欢吗?