-6

I've been working at Problem 03 of Project Euler as a Java novice. (Find the largest prime factor of 600851475143.) It was my first time using ArrayLists and a few confusions came up - hopefully these can be cleared up!

public static ArrayList<Integer> primeList (int n){
    ArrayList<Integer> prime = new ArrayList<Integer> ();
    for (int i = 2; i <= n; i++) {
        prime.add(i);
    }

    // Sieb des Eratosthenes
    for (int i = 2; i <= Math.sqrt(n); i++) {
        for (Integer j : prime) {
            if (j > i && j % i == 0) {
                prime.remove (Integer.valueOf(j)); // some error.
            }
        }
    }

    return prime;
}

This is my code for the Sieve of Eratosthenes. It's likely not very optimal, I know that - I will still continue to work on optimizing my algorithm.

However, what really bothers me is that this one doesn't compile. Somehow, it does compile for n = 2, 3, 5. For most other values I've tried, it does not.

Why is that? And more specifically, where is the error?

I was able to make it run with a "normal" for-loop instead of the for each-loop. That was a happy end, at least, I guess.

EDIT: By evaluating it in a main with Sys.out(primeList(9)), I get the error message:

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:907)
    at java.util.ArrayList$Itr.next(ArrayList.java:857)
    at problem03.TestPrime.primeList(TestPrime.java:18)
    at problem03.TestPrime.main(TestPrime.java:7)
Kezer
  • 148
  • 7
  • What do you mean by saying it doesn't compile? What errors do you get? – Vasilis G. Oct 29 '17 at 18:57
  • Do you know what "compile" means? – Thomas Böhm Oct 29 '17 at 18:58
  • *more specifically, where is the error?* Funny, I was about to ask you the same. – shmosel Oct 29 '17 at 18:58
  • I have downvoted this question because "some error" is not a useful error message. Please [edit] your question to provide the *exact text* of the error message, and I will consider retracting the downvote. – Joe C Oct 29 '17 at 18:59
  • You realize that 600851475143 can't fit into an `int`, right? – shmosel Oct 29 '17 at 18:59
  • Uhm wow, that's kind of harsh... The error I get is "Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:907) at java.util.ArrayList$Itr.next(ArrayList.java:857) at problem03.TestPrime.primeList(TestPrime.java:18) at problem03.TestPrime.main(TestPrime.java:7)". The "some error." was a comment for myself, I didn't make it specifically for this question. Yes, I do realize the large number doesn't fit into an int, however as I've mentioned, there are errors for integers like 9. – Kezer Oct 29 '17 at 19:05
  • By "more specifically, where is the error?", I rather meant what causes the error. I could only guess from my tests that somewhere around the "inside"-for loop must be an error. Obviously I don't know exactly what the error is or else why would I ask? – Kezer Oct 29 '17 at 19:07
  • Duplicate: https://stackoverflow.com/questions/18448671/how-to-avoid-concurrentmodificationexception-while-removing-elements-from-arr – shmosel Oct 29 '17 at 19:17
  • Possible duplicate of [How to avoid "ConcurrentModificationException" while removing elements from \`ArrayList\` while iterating it?](https://stackoverflow.com/questions/18448671/how-to-avoid-concurrentmodificationexception-while-removing-elements-from-arr) – vanje Oct 29 '17 at 19:17
  • Is there any explanation as to why Sys.out does print (correct) arrays for n = 2, 3, 5? – Kezer Oct 29 '17 at 19:19
  • Presumably it's not calling `remove()`. Did you try debugging? – shmosel Oct 29 '17 at 19:21
  • I gotta admit, I got confused by the debugger. It has to call remove(), though, doesn't it? For n = 5, it prints [2, 3, 5], that means 4 has to be removed from the ArrayList. – Kezer Oct 29 '17 at 19:23

1 Answers1

1

You are trying to remove item from list, that you currently iterating on, thus, ConcurentModifiacationException occures.

See here: https://stackoverflow.com/a/18448699/8851887

zbyszekt
  • 151
  • 1
  • 10