-3

This exception rises up

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
    at noob.Prim_up_to.prim(Prim_up_to.java:72)
    at noob.Prim_up_to.<init>(Prim_up_to.java:21)
    at _ex.main(_ex.java:11)

Here is the relevant code: http://pastebin.com/w6d0hyXb

Why is it that this happens? I tried using iterator but to no avail. The same exception shows up nevertheless.

hmnhmn
  • 173
  • 2
  • 9
  • this is not an exact duplicate! The problem is a bit more complicated due to assignment of a list to another list. i.e.: It is not easy to spot but at some point in your code you make an assignment `ss = nss;` but inside your `for loop` you call `nss.add(new Integer(npp-lrpp));`, so as `ss = nss` you indeed modify the collection while iterating over it! Try using an iterator or deep copy `nss` to `ss` – Kostas Kryptos Dec 25 '14 at 23:33

1 Answers1

1

This exception gets raised when you modify an (in this case) ArrayList while iterating over it. If you must modify an ArrayList during the course of an iteration, consider using a ListIterator, which has an add and remove method.

sdzivanovich
  • 740
  • 3
  • 14