Why am I getting java.util.ConcurrentModificationException?

When I run the following code:

import java.util.LinkedList; class Tester { public static void main(String args[]) { LinkedList<String> list = new LinkedList<String>(); list.add(new String("suhail")); list.add(new String("gupta")); list.add(new String("ghazal")); list.add(new String("poetry")); list.add(new String("music")); list.add(new String("art")); try { for(String s : list) { list.add(0,"art"); list.remove(6); System.out.println(list); } }catch(Exception exc) { exc.printStackTrace(); } } } 

I get an exception that says:

 java.util.ConcurrentModificationException at java.util.LinkedList$ListItr.checkForComodification(Unknown Source) at java.util.LinkedList$ListItr.next(Unknown Source) at Tester.main(Tester.java:14) 

Why am I getting this exception?

Edit: tmpList - LinkedList, each of which node contains an object of type DepConfAttr. I am sorting tmpList based on memory (first highest memory), which is one of the attributes of the DepConfAttr object.

The code above reflects what I'm trying to achieve with the following code

 int size = tmpList.size(); int elementBC = 0; // element being checked int startIndex = 1; for (DepConfAttr dca : tmpList) { long maxMem = dca.getMemory(); // Let this be the maximum memory for(int i = startIndex ; i < size ; i++) { DepConfAttr dcaTmp = tmpList.get(i); if(maxMem < dcaTmp.getMemory()) { tmpList.add(elementBC, dcaTmp); tmpList.remove(i+1); maxMem = tmpList.get(elementBC).getMemory(); } } elementBC++; startIndex++; size--; } 
+4
source share
7 answers

Why am I getting this exception?

You remove an item from the list, except through an iterator, iterating over it. You also add to the list while you repeat it.

It’s not entirely clear what you are trying to achieve here, but in addition to parallel collections, you will always get an exception when you try to do this.

One common solution is to first create a copy of the list and iterate over it, changing the original along the way.

+10
source

When you iterate over a list, you cannot remove items from it. This throws an exception.

do:

 int size = list.size(); for (int i = 0 ; i< size ; i++) { list.add(0,"art"); list.remove(6); System.out.println(list); } 
+2
source

The problem is that you directly modify the List while Iterator trying to start it. The next time you point Iterator to iterate (implicitly, in a for loop), it notices that the List has changed from under it and throws an exception.

Instead, if you need to change the list as you move it, explicitly take an Iterator 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 cannot insert into the List while this is happening, and this will not allow you to delete arbitrary elements, only current ones.

+1
source

The problem is making changes to the list when using the implicit Iterator over it. For the cited code, the easiest solution is to not use the Iterator at all:

 for(int i=0; i<list.size(); i++) { list.add(0,"art"); list.remove(6); System.out.println(list); } 

You may need to post more realistic code to get advice for a better solution. If you want to remove the current item during iteration through the list, use the Iterator explicit loop and use the Iterator remove () method. In other cases, the best solution is to create a plan of changes within the cycle, but then its implementation. For example, during a loop above the list, create a removeList containing a list of indexes of the elements you want to remove. In a separate loop above removeList, remove these items from the list.

0
source

Because you are simultaneously (simultaneously) changing iterations through the collection. Java don't like this

Since you are not actually using s, you can use the standard for loop

 for(int i=0; i< list.size(); i++) { list.add(0,"art"); list.remove(6); System.out.println(list); } 
0
source

The ConcurrentModificationException parameter is generated when it is repeated in the list and at the same time when you are trying to change (add / remove) the contents of the list either through another thread or in a loop.

You can try using ConcurrentLinkedQueue or, as John said, make a copy and change the original in an 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
source

You will need to use an iterator explicitly for this. Example:

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

More information here: http://www.coderanch.com/t/233932/threads/java/deal-Concurrent-Modification-Exception , but just a google exception and you will find many examples.

-one
source

Source: https://habr.com/ru/post/1498921/


All Articles