Why am I getting java.util.ConcurrentModificationException?

前端 未结 7 908
独厮守ぢ
独厮守ぢ 2020-12-19 10:55

As I run the following code :

    import java.util.LinkedList;

    class Tester {
      public static void main(String args[]) {
        LinkedList

        
相关标签:
7条回答
  • 2020-12-19 11:25

    When you iterate through a list, you can't remove items from it. Doing so causes the exception.

    Do:

    int size = list.size();
    for (int i = 0 ; i< size ; i++) {
       list.add(0,"art");
       list.remove(6);
       System.out.println(list);
    }
    
    0 讨论(0)
  • 2020-12-19 11:28

    Why am I getting this exception ?

    You're removing an item from the list other than via the iterator, while iterating over it. You're also adding to the list while you're iterating over it.

    It's not really clear what you're trying to achieve here, but other than with the concurrent collections, you'll always get an exception when you try to do that.

    One common fix to this is to create a copy of the list first and iterate over that, modifying the original as you go.

    0 讨论(0)
  • 2020-12-19 11:28

    The ConcurrentModificationException is thrown when iterating through the list and at the same time when you are trying to modify (add/remove) the contents of the list either through another thread or in a loop.

    You can try using ConcurrentLinkedQueue or as John stated do a copy and modify the original in iteration.

    Queue<String> list = new ConcurrentLinkedQueue<String>();
    
    list.add("suhail");
    list.add("gupta");
    list.add("ghazal");
    list.add("poetry");
    list.add("music");
    list.add("art");
    
    int size = list.size();
    
    for(int i = 0; i < size; i++){
        list.add("art");
        list.remove("art");
        System.out.println(list);
    }
    
    0 讨论(0)
  • 2020-12-19 11:33

    Because you are concurrently (simultaneously) modifying and iterating through a collection. Java doesn't like this

    Since you don't actually use s, you can use a standard for loop

    for(int i=0; i< list.size(); i++) {
          list.add(0,"art");
          list.remove(6);
          System.out.println(list);
    }
    
    0 讨论(0)
  • 2020-12-19 11:34

    The problem is that you're directly modifying the List while an Iterator is trying to run over it. The next time you tell the Iterator to iterate (implicitly, in the for loop), it notices the List has changed out from under it and throws the exception.

    Instead, if you need to modify the list while traversing it, grab the Iterator explicitly and use it:

    List<String> list = ....
    Iterator<String> iterator = list.iterator();
    while (iterator.hasNext()) {
        String s = iterator.next(); // must be called before you can call iterator.remove()
        iterator.remove();
    }
    

    You still can't insert into the List while this is going on, and this won't let you remove arbitrary elements, just the current one.

    0 讨论(0)
  • 2020-12-19 11:42

    You would need to use iterator explicitly for that to work. Example:

     Iterator<String> iter = li.iterator();
       while(iter.hasNext()){
        if(iter.next().equalsIgnoreCase("some value"))
          iter.remove();
        }
       }
    

    More info here: http://www.coderanch.com/t/233932/threads/java/deal-Concurrent-Modification-Exception but just google the exception and you will find lots of examples.

    0 讨论(0)
提交回复
热议问题