2016-09-27 57 views
0

下面的代碼是導致使用索引中移除元素的ArrayList中引起java.util.ConcurrentModificationException

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

     } 

完整的方法外循環是不相關的列表

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

     } 

    } 

} 
+0

你需要用戶迭代器, –

+0

這裏沒有迭代。 – shmosel

+0

沒有循環我只需要刪除第一個元素 –

回答

1

您可以創建一個不同的ArrayList和執行刪除操作存在或提出了一個迭代器對ArrayList和使用刪除迭代器。

找到幾個可能的解決方案來解決您的問題herehere