简体   繁体   English

删除项目时ArrayList迭代器并发修改异常

[英]Arraylist iterator concurrent modification exception when removing item

So this question is a little bit different from the others i've found on here about concurrent exception when modifying the list- because this happens when im modifying an internal list of an object within the list. 因此,此问题与我在此处发现的其他有关修改列表时的并发异常的问题有些不同-因为此问题发生在我修改列表中对象的内部列表时。 This is the only method accessing the internal list 这是访问内部列表的唯一方法

Here's where i call the method 这是我调用方法的地方

public void interactWithItem(int targetIDX, int targetIDY){
    for(Iterator<Item> it = listOfAllItems.iterator(); it.hasNext();){
        Item tempItem = it.next();
        //Maybe i should refine more, in world, so forth
        if(tempItem.tilePosX == targetIDX && tempItem.tilePosY == targetIDY){
            if(tempItem.name.equals("chest")){
                System.out.println("Interacting with Chest!");
                if(!tempItem.containedItems.isEmpty()){
                    for(Iterator<Item> tempIt = tempItem.containedItems.iterator(); tempIt.hasNext();){
                        Item tItem = tempIt.next();
                        System.out.println("Chest contains "+tItem.name+" "+tItem.amount);
                        Character.c.addItem("player", tItem.name, tItem.amount);
                        removeContainedItem("chest", tItem.name);
                    }
                }else{
                    System.out.println("Chest is empty");
                }
            }
        }
    }
}

Here's the method that causes the issue, if i comment out the i.remove(); 如果我注释掉i.remove(),这就是导致问题的方法; the issue seizes to happen- so its only upon removal, yet no other method or class is accessing the internal list ? 这个问题抓住了发生,所以只有将其删除后,再没有其他方法或类正在访问内部列表?

public void removeContainedItem(String containerName, String itemName){

    System.out.println("Removing "+itemName+" in "+containerName);
    for(Iterator<Item> it = listOfAllItems.iterator(); it.hasNext();){
            Item tItem = it.next();
        if(tItem.name.equals(containerName)){
            for(Iterator<Item> i = tItem.containedItems.iterator(); i.hasNext();){
                Item tempItem = i.next();
                System.out.println(tempItem.name);
                if(tempItem.name.equals(itemName)){
                    i.remove();
                }
            }
        }
    }
}

Thanks for all the help! 感谢您的所有帮助! Hope someone can clarify and give me instructions as to how i might go about fixing this thing? 希望有人可以澄清并给我有关如何解决此问题的说明? Im a bit at a loss. 我有点茫然。

Concurrent Modification Exception occurs when a collection is modified between the iterations. 在迭代之间修改集合时,会发生并发修改异常。 We can use ConcurrentHashMap or CopyOnWriteArrayList to overcome this issue. 我们可以使用ConcurrentHashMap或CopyOnWriteArrayList来解决此问题。

If hitting UnsupportedOperationException, change the ArrayList to LinkedList 如果遇到UnsupportedOperationException,请将ArrayList更改为LinkedList

Might be : 可能 :

**Item tItem = tempIt.next();**

When you create an item you then add it to more than one list. 创建项目时,您可以将其添加到多个列表中。 And when one of them tries to modify it you can get exceptions. 当其中之一尝试修改它时,您会得到例外。 Because bouth lists are using the same item, exactly the same one. 由于清单列表使用的是同一项目,因此完全相同。

Fix that might help would be : 可能会帮助解决的问题是:

Item newItem = new Item();
newItem  = tempIt.next();

Simply create new item for each list and modify them as you please. 只需为每个列表创建一个新项目,然后根据需要对其进行修改。

Or create a new list for : 或为创建新列表:

public void removeContainedItem(String containerName, String itemName){

And modify new copy list with items, then set previous list to modified one. 并使用项目修改新的副本列表,然后将上一个列表设置为已修改的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM