以下代码导致
java.util.ConcurrentModificationException
我不知道如何解决这个错误,请帮忙!
ArrayList strList = new ArrayList<String>(Arrays.asList(cmd.split(" ")));
if (strList.get(0).equals("LIST")) {
}
if (strList.get(0).equals("DEPEND")) {
strList.remove(0); // getting error at this point
cm.createComponent(strList);
}
完整方法外层循环与List无关
public static void main(String[] args) throws IOException {
ComponentManager cm = new ComponentManager();
List<String> lines = Files.readAllLines(Paths.get("cmdList.txt"));
for (String cmd : lines) {
ArrayList strList = new ArrayList<String>(Arrays.asList(cmd.split(" ")));
if (strList.get(0).equals("LIST")) {
}
if (strList.get(0).equals("DEPEND")) {
strList.remove(0);
cm.createComponent(strList);
}
if (strList.get(0).equals("INSTALL")) {
}
if (strList.get(0).equals("REMOVE")) {
}
}
}
最佳答案
关于java - 使用 INDEX 删除 ArrayList 中的元素导致 java.util.ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39717058/