Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA.util.ConcurrentModificationException_Java - Fatal编程技术网

JAVA.util.ConcurrentModificationException

JAVA.util.ConcurrentModificationException,java,Java,我正在尝试创建简单的JAVA程序。但从上一天开始,我就有这个问题,我无法解决 public void receiveUpdate(ArrayList<Connection> connections) { for(Connection senderConnection : connections) { for(Connection receiverConnection : this.connections) { if(senderC

我正在尝试创建简单的JAVA程序。但从上一天开始,我就有这个问题,我无法解决

public void receiveUpdate(ArrayList<Connection> connections) {

    for(Connection senderConnection : connections) {

        for(Connection receiverConnection : this.connections) {

            if(senderConnection.getDestination() == receiverConnection.getDestination()) {

                if(senderConnection.getDistance() + 1 < receiverConnection.getDistance()) {

                    senderConnection.setEnabled(false);
                    senderConnection.setDistance(senderConnection.getDistance() + 1);

                    this.connections.remove(receiverConnection);
                    this.connections.add(senderConnection);
                }
            }
        }

        senderConnection.setEnabled(false);
        senderConnection.setDistance(senderConnection.getDistance() + 1);

        this.connections.add(senderConnection);
    }
}
我注意到,如果我添加注释块,则不会出现该异常:

senderConnection.setEnabled(false);
senderConnection.setDistance(senderConnection.getDistance() + 1);

this.connections.add(senderConnection);

有谁能告诉我,问题出在哪里吗?

您正在循环
这个.connections
并且您正在尝试从
这个.connections中删除。这个将
java.util.ConcurrentModificationException
。将您的代码更改为使用迭代器。

您正在循环
此.connections
并尝试从
此中删除内部连接。此连接将
java.util.ConcurrentModificationException
。将您的代码更改为使用迭代器。

您正在循环
此.connections
并尝试从
此中删除内部连接。此连接将
java.util.ConcurrentModificationException
。将您的代码更改为使用迭代器。

您正在循环
此.connections
并尝试从
此中删除内部连接。此连接将
java.util.ConcurrentModificationException
。将代码更改为使用迭代器。

您实际上是在迭代时修改集合

this.connections.remove(receiverConnection);
this.connections.add(senderConnection);
使用和呼叫:

Iterator iter=connections.Iterator();
while(iter.hasNext()){
如果(某些条件)
iter.remove();
}

使用迭代器删除方法而不是ArrayList

您实际上是在迭代时修改集合

this.connections.remove(receiverConnection);
this.connections.add(senderConnection);
使用和呼叫:

Iterator iter=connections.Iterator();
while(iter.hasNext()){
如果(某些条件)
iter.remove();
}

使用迭代器删除方法而不是ArrayList

您实际上是在迭代时修改集合

this.connections.remove(receiverConnection);
this.connections.add(senderConnection);
使用和呼叫:

Iterator iter=connections.Iterator();
while(iter.hasNext()){
如果(某些条件)
iter.remove();
}

使用迭代器删除方法而不是ArrayList

您实际上是在迭代时修改集合

this.connections.remove(receiverConnection);
this.connections.add(senderConnection);
使用和呼叫:

Iterator iter=connections.Iterator();
while(iter.hasNext()){
如果(某些条件)
iter.remove();
}

使用迭代器删除方法而不是ArrayList

ConcurrentModificationException此异常可能由检测到对象并发修改的方法引发,如果不允许此类修改。 问题就在这里

this.connections.remove(receiverConnection);
当对象的并发修改不允许时,检测到该修改的方法可能会引发此异常。 例如,通常不允许一个线程在另一个线程迭代集合时修改集合。通常,在这些情况下,迭代的结果是未定义的。如果检测到此行为,某些迭代器实现(包括JRE提供的所有通用集合实现)可能会选择抛出此异常。这样做的迭代器称为fail-fast迭代器,因为它们快速、干净地失败,而不是在将来的不确定时间冒任意、不确定行为的风险

请注意,此异常并不总是表示对象已被其他线程并发修改。如果单个线程发出一系列违反对象约定的方法调用,该对象可能会引发此异常。例如,如果线程在使用fail fast迭代器迭代集合时直接修改集合,迭代器将引发此异常

退房

要解决您的问题,您需要使用
Iterator
remove()
这样的方法

Iterator<String> connectionsIterator = connections.iterator();

while (connectionsIterator.hasNext()) {

    if (senderConnection.getDistance() + 1 < receiverConnection.getDistance()){

        connectionsIterator.remove();
// add the connection to another list other than this.connections.add(senderConnection);
// then when you finish add them back the this.connections
// so you avoid modifying this.connections
   }
} 
迭代器连接迭代器=连接。迭代器();
while(Connection迭代器.hasNext()){
if(senderConnection.getDistance()+1
ConcurrentModificationException此异常可能由检测到对象的并发修改的方法引发,但不允许此类修改。 问题就在这里

this.connections.remove(receiverConnection);
当对象的并发修改不允许时,检测到该修改的方法可能会引发此异常。 例如,通常不允许一个线程在另一个线程迭代集合时修改集合。通常,在这些情况下,迭代的结果是未定义的。如果检测到此行为,某些迭代器实现(包括JRE提供的所有通用集合实现)可能会选择抛出此异常。这样做的迭代器称为fail-fast迭代器,因为它们快速、干净地失败,而不是在将来的不确定时间冒任意、不确定行为的风险

请注意,此异常并不总是表示对象已被其他线程并发修改。如果单个线程发出一系列违反对象约定的方法调用,该对象可能会引发此异常。例如,如果线程在使用fail fast迭代器迭代集合时直接修改集合,迭代器将引发此异常

退房

要解决您的问题,您需要使用
Iterator
remove()
这样的方法

Iterator<String> connectionsIterator = connections.iterator();

while (connectionsIterator.hasNext()) {

    if (senderConnection.getDistance() + 1 < receiverConnection.getDistance()){

        connectionsIterator.remove();
// add the connection to another list other than this.connections.add(senderConnection);
// then when you finish add them back the this.connections
// so you avoid modifying this.connections
   }
} 
迭代器连接迭代器=连接。迭代器();
while(Connection迭代器.hasNext()){
if(senderConnection.getDistance()+1