how to overcome ConcurrentModificationException error

Go To


Possible Duplicate:
Concurrent modification error when adding elements to LinkedList

i've a arraylist of linkedlist

List<LinkedList<File1>> backup = new ArrayList<LinkedList<File1>>();

during runtime on clicking a button i want to remove few elements from a list and the add them in other list. i also tried declaring the linked list as

List<LinkedList<File1>> lists = Collections.synchronizedList(new ArrayList<LinkedList<File1>>());

bt still the error remains the same . im not able to figure out the error , im also new to java so plz do help . My code is as follows.

 private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // When delete button is clicked
        int parent_node = 0, d =0 ;
        File1 srt[] ;
        File1 deleted[] = new File1[no_node];
        Random randomGenerator = new Random();
        int rand_no = 0;
        while(rand_no == 0)
            rand_no = randomGenerator.nextInt(no_node);
        System.out.println("random no : " + rand_no);
             ListIterator itr = it_bk.get(i);
                 File1 file = (File1);                 
                 if(rand_no == file.nod)
                     parent_node = i;
             //iterating back
                  File1 file = (File1) itr.previous();

         System.out.println("Parent node:" + parent_node);
         //add the nodes to parent
         ListIterator itr1 = it_bk.get(rand_no);

         System.out.println("randam node");

             File1 file = (File1);
             deleted[d] = new File1();
             deleted[d] = file ;
             System.out.println("node :" + file.nod + "\tdist :" + file.dist);

         LinkedList<File1> tmp_bk = backup.get(parent_node);
             System.out.println(deleted[j].nod + "\t" + deleted[j].dist);
         ListIterator itr_p = it_bk.get(parent_node);
         System.out.println("parent node");
             File1 file = (File1);
             System.out.println("node :" + file.nod + "\tdist :" + file.dist);

2012-04-04 07:32
by Divyashree
you cannot modify a list while iterating over it. Think about it. you have 5 items and iterating over it, then you remove 1 item. You are making the state of the list inconsistent - zengr 2012-04-04 07:37


You are traversing through the list and modifying the same list that is not valid,


Manage two list instead, and after iteration apply effect of resultant list to the source list

2012-04-04 07:35
by Jigar Joshi
thnks a lot for ur reply :) but i dont hav a prob in removing the elements in the list . that is the reason i put the elements into a array and then im trying to add there elements into another list - Divyashree 2012-04-11 15:47
thnks a lot :) its working nw : - Divyashree 2012-04-13 07:57
you are welcome :) (also to mark this answer as accepted), if this has really answered your questio - Jigar Joshi 2012-04-13 08:30


The error occurs when you remove objects from a List while iterating over it. You could put the items you want to remove in another list and after the iteration over the first one just call firstlist.removeAll(list_with_items_to_remove).

2012-04-04 07:37
by Magnus Winter
Which can be accomplished with a copy of the list and the methods removeAll (or perhaps retainAll in some cases - keyser 2012-04-04 07:38
ok if that s the prob i removed it . now im oly adding Objects to the linkedlist and i still get the same error - Divyashree 2012-04-11 19:35
You can't insert objects to a list while iterating over it either. When you start the iteration you need to consider the List locked for inserts and removals. All those need to be done after the iteration. Do the same thing as in my answer but use firstlist.addAll(listwithitemstoadd) instead. If you REALLY need to modify the list while iterating you could look at java.util.concurrent.ConcurrentLinkedQueue or something like it. But be aware that it's not as fast as iterating over a normal list - Magnus Winter 2012-04-12 14:12
ya i understood .. im nt iterating through the list .. here is the code to add more elements - Divyashree 2012-04-12 17:18
i understood , its working nw thnks a lot : - Divyashree 2012-04-13 07:57
If you found this helping you should mark the answer as correct. And good luck in the future :- - Magnus Winter 2012-04-13 14:19