我收到以下异常:“java.util.ConcurrentModificationException”
当我运行这段代码时,出现“发生异常:java.util.ConcurrentModificationException”。这里有人看到问题是什么吗?
public void mudaDeEstado() {
Luz luz = new Luz();
while(this.iterador.hasNext()) {
luz = (this.iterador.next());
luz.defineEstado(!luz.acesa());
}
}
多谢!!
I'm getting a "An exception occurred: java.util.ConcurrentModificationException" when I run this piece of code. Does anyone here see what the problem is?
public void mudaDeEstado() {
Luz luz = new Luz();
while(this.iterador.hasNext()) {
luz = (this.iterador.next());
luz.defineEstado(!luz.acesa());
}
}
Thanks a lot!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您试图修改迭代器在循环元素时保存的引用。您可以在此处阅读有关他的异常的更多信息。
最有可能的罪魁祸首是这样的:
You are trying to modify the reference that the iterator holds while looping through the elements. You can read more about his exception here.
Most probably the culprit here is this:
当您在迭代数据结构时修改数据结构时,会引发此异常。更改数据结构中的元素可以改变迭代元素的方式,因此许多数据结构不允许并发修改。
尝试保留需要更新的元素列表,然后在迭代整个数据结构后返回并更新这些元素。
抱歉,我的措辞有点笼统且含糊不清,但很难用所提供的代码给出具体细节。
This exception is thrown when you modify a data structure while you are iterating through it. Changing the elements in the data structure can change the way one iterates through the elements, so many data structures do not allow concurrent modification.
Try keeping a list of elements that need to be updated and then go back through and update those elements once you have iterated through the entire data structure.
Sorry my wording is kind of general and ambiguous, but it's hard to give specifics with the provided code.