0

The following code is causing

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")) {

        }

    }

}
Nikhil Kulkarni
  • 644
  • 1
  • 6
  • 23

1 Answers1

1

You can create a different ArrayList and perform remove operation there or put up an iterator on the arrayList and remove using the iterator.

Find a couple of potential solutions to your problem here and here.

Community
  • 1
  • 1
Akash Mishra
  • 682
  • 1
  • 5
  • 13