java ArrayList 中的 concurrentModificationException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26955041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
concurrentModificationException in ArrayList
提问by SouvikMaji
Problem statement: Design a system for the following scenario:
问题陈述:为以下场景设计一个系统:
1.An item list contains item code, name, rate, and quantity for several items.
Whenever a new item is added in the list uniqueness of item code is to be checked. Register a new product with its price.
Time to time rate of the items may change.
Whenever an item is issued or received existence of the item is checked and quantity is updated.
In case of issue, availability of quantity is also to be checked.
User may also like to know price/quantity available for an item.
Find how many items cost more than a given amount. The amount will be a parameter.
Remember that the methods have to return an error code if for example an invalid item code is given
1.商品清单包含若干商品的商品代码、名称、费率和数量。
每当在列表中添加新项目时,都要检查项目代码的唯一性。以价格注册新产品。
项目的时间率可能会发生变化。
每当发出或接收物品时,都会检查物品是否存在并更新数量。
如果出现问题,还应检查数量的可用性。
用户可能还想知道商品的可用价格/数量。
找出有多少物品的价格超过了给定的金额。数量将是一个参数。
请记住,如果例如给出了无效的项目代码,这些方法必须返回错误代码
For the above problem i have created a Itemclass with following members:
对于上述问题,我创建了一个具有以下成员的Item类:
private String name;
private double rate;
private long code;
private int quantity;
public Item()
public Item(String name, double rate, long code, int quantity)
public Item(Item item)
public String toString()
public String getName()
public void setName(String name)
public double getRate()
public void setRate(double rate)
public long getCode()
public void setCode(long code)
public int getQuantity()
public void setQuantity(int quantity)
Now I have created a Shop classto access the Item class, to do all operations on a Item... Here is a part of the shop class.
现在我创建了一个Shop 类来访问Item 类,对一个 Item 进行所有操作......这是 shop 类的一部分。
private ArrayList<Item> ItemList;
private Iterator<Item> itr;
public Shop() {
System.out.println("New Shop for Items created.");
ItemList = new ArrayList<Item>();
itr= ItemList.iterator();
}
public Item search(long code) {
Item item;
while(itr.hasNext()) {
item = new Item(itr.next());
if (item.getCode() == code) {
return item;
}
}
return null;
}
public void addItem() throws InputMisMatchEXception{
long aCode;
String aName;
double aRate;
int aQuantity;
Item foundItem;
System.out.println("Enter Item code:");
aCode = sc.nextLong();
foundItem = search(aCode);
if (foundItem == null) {
System.out.println("Item name : ");
aName = sc.next();
System.out.println("Rate : ");
aRate = sc.nextDouble();
System.out.println("Quantity : ");
aQuantity = sc.nextInt();
Item aItem = new Item(aName, aRate, aCode, aQuantity);
ItemList.add(aItem);
} else if (foundItem != null) {
System.out.println("Item exists");
}
}
Now when i am adding a new item, it works.. But When I add a second element it throws a
现在,当我添加一个新项目时,它可以工作.. 但是当我添加第二个元素时,它会抛出一个
ConcurrentModificationException
并发修改异常
Here is a sample output:
New Shop for Items created.
*
这是一个示例输出:已
创建商品的新商店。
*
-----ITEM------
1. Add items to list
2. Update item list
3. Issue item
4. Display item details
5. Exit
Choice: 1
Enter Item code: 123
Item name : qwerty
Rate : 12345
Quantity : 100
1. Add items to list
2. Update item list
3. Issue item
4. Display item details
5. Exit
Choice: 1
Enter Item code: 1212
Exception in thread "main"
java.util.ConcurrentModificationException at
java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901) at
java.util.ArrayList$Itr.next(ArrayList.java:851) at
items.Shop.search(Shop.java:28) at items.Shop.addItem(Shop.java:55)
at items.ItemDemo.main(ItemDemo.java:26)
-----ITEM------
1. 将项目添加到列表
2. 更新项目列表
3. 发布项目
4. 显示项目详细信息
5. 退出
选择:1
输入项目代码:123
项目名称:qwerty
比率:12345
数量:100
1. 将项目添加到列表
2. 更新项目列表
3. 发布项目
4. 显示项目详细信息
5. 退出
选择: 1
输入项目代码: 1212
线程“main”
中的异常java.util.ConcurrentModificationException at
java.util。 ArrayList$Itr.checkForCommodification(ArrayList.java:901) 在
java.util.ArrayList$Itr.next(ArrayList.java:851) 在
items.Shop.search(Shop.java:28) at items.Shop.addItem(Shop.java:55)
at items.ItemDemo.main(ItemDemo.java:26)
What am i doing wrong? (Tried to give all relevant classes)
我究竟做错了什么?(尝试提供所有相关课程)
回答by JB Nizet
You're ading an item to your list while iterating on it. That is not valid unless you use the iterator itselfto add the new Item to the list.
您在迭代时将一个项目添加到您的列表中。除非您使用迭代器本身将新 Item 添加到列表中,否则这是无效的。
Note that it's probably an error to use the same iterator everyt time the search()
method is called. The iterator should probably be a local variable of the search()
method rather than a field.
请注意,每次search()
调用该方法时都使用相同的迭代器可能是错误的。迭代器可能应该是方法的局部变量search()
而不是字段。
回答by Xline
Insearch(long code)
method just add
在search(long code)
方法中只需添加
itr = ItemList.iterator();
before
前
while(itr.hasNext())
This is because you change the list while the iterator still pointing to the old list. once you add items without updating the iterator you will get the concurrent exception.
这是因为您在迭代器仍指向旧列表时更改了列表。一旦添加项目而不更新迭代器,您将获得并发异常。
回答by Ashutosh Kumar
It comes when we add the elements to the list after declaring an iterator obejct. Once try to add all the elemnts to the list and after that declare the iterator object.
当我们在声明迭代器对象后将元素添加到列表中时,它就会出现。一旦尝试将所有元素添加到列表中,然后声明迭代器对象。