1

I'm trying to do some caluclation for each value of the list in the main thread as below

for (int i = 0; i < list.size(); i++) {
                //Do some task
            }

Bur I'm getting the ConcurrentModificationException on list.size().

It is the simple java.util.List

Caused by: java.util.ConcurrentModificationException
                                                                        at java.util.AbstractList$SubAbstractList.size(AbstractList.java:360)
                                                                        at java.util.Collections$SynchronizedCollection.size(Collections.java:440)

How to solve this ?

Complete code :

private List<Double> computeValue(@NonNull List<Double> list1, List<Double> list2) {
        List<Double> computedList = new ArrayList<>();
            for (int i = 0; i < list1.size(); i++) {
                computedList.add(-((list1.get(i) + list2.get(i)) / 2));
            }
        return computedList;
    }
Praveen
  • 465
  • 5
  • 13
  • Are you deleting elements in the list while you are iterating over it? Please show the code that comes in "//Do some task". – hamena314 Jun 14 '17 at 10:17
  • @hamena314 please check the edit, I've added the complete code and am not changing the values. – Praveen Jun 14 '17 at 10:23
  • I am not aware of an implementation of `List` which would throw a CME on size, because that's not part of an iteration. Are you sure it is occurring there? What are the concrete types of the lists? – Andy Turner Jun 14 '17 at 10:32
  • I think it is something Android specific. I cannot see class called `SubAbstractList` in Java, not to mention that `size()` method is abstract in `AbstractList`. – Jaroslaw Pawlak Jun 14 '17 at 10:37
  • @AndyTurner it is simple java.util.List, it is throwing CME list.size() as mentioned in the description – Praveen Jun 14 '17 at 10:41
  • @JaroslawPawlak even I'm confused with it, but it is throwing exception on list.size() and it is android – Praveen Jun 14 '17 at 10:45
  • @Praveen `java.util.List` is an interface. You cannot be using it directly, you must be using something which implements the interface (e.g. `java.util.ArrayList`). What is the concrete your of the `List` instances? – Andy Turner Jun 14 '17 at 10:47
  • @AndyTurner List is of Double type and of size 610, and I'm not aware of the concrete type meaning – Praveen Jun 14 '17 at 10:53

2 Answers2

1

Use ListIterator to solve the ConcurrentModificationException. In ListIterator you can add while iterating.

Eg:

  List<Double> list = new ArrayList<>();
  list.add(2d);
  list.add(3d);
  list.add(4d);
  ListIterator iterator = list.listIterator();
  while (iterator.hasNext()){
     double val = (double) iterator.next();
     iterator.add(val + val/2);
  }
  System.out.println(list);

Out put

 [2.0, 3.0, 3.0, 4.5, 4.0, 6.0]
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

I'm not sure what was the issue, but it is solved by checking for the size outside the for loop. Anyhow thanks for the reply

int listSize = list.size();     
for (int i = 0; i < listSize; i++) {
    //Do some task
}
Praveen
  • 465
  • 5
  • 13
  • Looking at your question and your answer, you seem to use 4 different lists, namely `list`, `list1`, `list2`, `computedList`. In your first version of the question, you used `list`, in the "Complete code" you use `list1`. In your answer you use `list`. I think you are using abbreviated code to show your problem, but is it possible, that you confused some list-names and simply posted the wrong code? I'm just curious. – hamena314 Jun 14 '17 at 13:47
  • @hamena314 I'm using list1, list2 and computedList. list is the example I have given – Praveen Jun 14 '17 at 15:55