-2

Having read other questions on here I believe I would be right in saying that this error occurs when you are trying to modify the children of a node while iterating over it.

I saw a couple of methods I could use to overcome this issue, however when I was messing around with my code I noticed that by replacing my enhanced for loop with a regular for loop it solved the issue.

My first question is why does this work? It would seem to me that I am still modifying the children of a node while iterating over them, so I don't understand why this now works. Secondly, is there anything wrong with overcoming the error in this way?

Update

As requested, here is a snippet of code

    ArrayList<Button> list = new ArrayList<Button>();
    Button b1 = new Button();
    Button b2 = new Button();
    Button b3 = new Button();
    Collections.addAll(list, b1, b2, b3);

    //Error
    for(Button b : list) {
        list.remove(b);
    }

    //No Error
    for(int i = 0; i < list.size(); i++) {
        Button b = list.get(i);
        list.remove(b);
    }
kleopatra
  • 51,061
  • 28
  • 99
  • 211
user2099816
  • 231
  • 1
  • 3
  • 7
  • Could you show us your code and especially this loop? – deHaar Mar 27 '19 at 08:01
  • 2
    Please add some [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Mohamed Benmahdjoub Mar 27 '19 at 08:01
  • We need to see your code, I'm not sure what you are doing. But as a general rule, if you want to iterate over a list, and also remove items from that list, probably the best way to do it is with an [Itterator.remove()](https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#remove--) – Kalec Mar 27 '19 at 08:04
  • I have updated with code. – user2099816 Mar 27 '19 at 08:15
  • Have you checked the content of the list after the `ConcurrentModificationException`? Are all elements still present? I tested it with a `List` and the enhanced `for`-loop threw such an `Exception` but also **removed an element that was shouldn't have been removed**. You can easily achieve what you want in Java 8 using `list.removeIf(/* your condition */);` If you don't need a condition and want to remove all the elements like in your loops, you can just write `list = new ArrayList()`; – deHaar Mar 27 '19 at 08:36
  • a) this is unrelated to javafx, just plain java list behavior b) while the second will not bark, it will not do what you expect (at least I assume that you expect ;) Hint: check the size of the list after the loop ends – kleopatra Mar 27 '19 at 09:42

2 Answers2

1

The best way to remove an element from a list is using an iterator

Note that Iterator.remove() is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.

You can find more in this stackoverflow thread

octopus
  • 319
  • 1
  • 4
  • 16
0

On the first loop you get an error because you are trying to modify a collection that you are currently traversing.

The second one works because instead of traversing the collection, you are getting an item from the collection then calling remove on it.

Check this: https://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html

sechanakira
  • 243
  • 3
  • 11
  • I see, it seems really obvious now pointed out! Any idea on my second question, whether this is bad practice, or if I am good to use this? – user2099816 Mar 27 '19 at 08:43