This question already has answers here:
Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop
(27个答案)
1年前关闭。
根据运行时错误消息,异常发生在下一行;
字符串vName 字符串vDefinition
重载
带有标记化定义的数组(以xLength组分隔) 带有LCS token 的数组 以 等级 float
最后,使用上述所有方法的方法都会发生错误:
我真的可以提供一些有关如何解决此问题的帮助和建议,以使程序成功运行。波纹管,完整的StackTrace:
从
如果您打算在每次调用该方法时都对整个数据库进行迭代,则建议您这样做
(27个答案)
1年前关闭。
根据运行时错误消息,异常发生在下一行;
VirusData v = iteratorVirusDB.next();
VirusData
是带有构造函数和重载构造函数的类,其中包含有关数据库中每种病毒的特定信息,例如;重载
带有标记化定义的
iteratorVirusDB
类型的<VirusData>
是VirusDB
的.iterator(),如下所示:Iterator<VirusData> iteratorVirusDB = virusDB.iterator();
VirusDB
是和<VirusData>
类型的ArrayList,我在其中存储病毒对象(此时为name和def),以便以后使用。ArrayList <VirusData> virusDB = new ArrayList<VirusData>();
最后,使用上述所有方法的方法都会发生错误:
private void selectDabataseMouseClicked(java.awt.event.MouseEvent evt) {
while(iteratorVirusDB.hasNext()) {
VirusData v = iteratorVirusDB.next(); //ERROR LINE
String vSig = v.signature;
v.tokens = tokenize.raw(vSig, true, tLength);
...
}
...
}
我真的可以提供一些有关如何解决此问题的帮助和建议,以使程序成功运行。波纹管,完整的StackTrace:
run:
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at project_clean.Main.selectDabataseMouseClicked(Main.java:275)
at project_clean.Main.access$100(Main.java:11)
at project_clean.Main$2.mouseClicked(Main.java:76)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
at java.awt.Component.processMouseEvent(Component.java:6270)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
最佳答案
显而易见的解释是您在两次调用之间修改了virusDB
。使用迭代器进行迭代时,不得修改 vector (通过Iterator
/ListIterator
方法除外)。
此代码段将始终抛出ConcurrentModificationException
:
import java.util.*;
class VirusData {
}
public class Test {
public static void main(String[] args) {
List<VirusData> list = new ArrayList<VirusData>() {{
add(new VirusData());
add(new VirusData());
add(new VirusData());
}};
Iterator<VirusData> iterator = list.iterator();
iterator.next();
list.remove(0);
VirusData s = iterator.next();
}
}
从
ConcurrentModificationException
的文档中:如果您打算在每次调用该方法时都对整个数据库进行迭代,则建议您这样做
private void selectDabataseMouseClicked(java.awt.event.MouseEvent evt) {
Iterator<VirusData> iteratorVirusDB = virusDB.iterator();
while(iteratorVirusDB.hasNext()) {
VirusData v = iteratorVirusDB.next();
String vSig = v.signature;
07-26 06:29