简体   繁体   English

使用INDEX删除ArrayList中的元素,从而导致java.util.ConcurrentModificationException

[英]Removing element in ArrayList using INDEX causing java.util.ConcurrentModificationException

The following code is causing 以下代码导致

java.util.ConcurrentModificationException java.util.ConcurrentModificationException

I am not sure how to solve the error please help ! 我不确定如何解决错误,请帮忙!

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);

        }

Full Method The outer loop is not related to the 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")) {

        }

    }

}

You can create a different ArrayList and perform remove operation there or put up an iterator on the arrayList and remove using the iterator. 您可以创建一个不同的ArrayList并在那里执行删除操作,或者在arrayList上放置一个迭代器,然后使用该迭代器进行删除。

Find a couple of potential solutions to your problem here and here . 在这里这里找到一些可能解决您问题的方法。

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

相关问题 ArrayList上的java.util.ConcurrentModificationException - java.util.ConcurrentModificationException on ArrayList java.util.ConcurrentModificationException - ArrayList - java.util.ConcurrentModificationException - ArrayList 删除列表元素后的java.util.ConcurrentModificationException - java.util.ConcurrentModificationException after removing an element of a list 从HashMap中删除元素时发生异常java.util.ConcurrentModificationException - Exception when removing element from HashMap java.util.ConcurrentModificationException 使用 ArrayList 和递归避免 java.util.ConcurrentModificationException - avoid java.util.ConcurrentModificationException using ArrayList and recursion 带有自定义ArrayList的java.util.ConcurrentModificationException - java.util.ConcurrentModificationException with custom ArrayList 多线程ArrayList上的java.util.ConcurrentModificationException - java.util.ConcurrentModificationException on ArrayList in multi thread ArrayList处理中的java.util.ConcurrentModificationException - java.util.ConcurrentModificationException in ArrayList processing java.util.ConcurrentModificationException但我没有删除 - java.util.ConcurrentModificationException But I am not removing java.util.ConcurrentModificationException 从 arraylist 中删除元素时,即使使用迭代器 - java.util.ConcurrentModificationException when removing elements from arraylist even with iterators
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM