1

I'm working on a project for school but i'm a little stuck right now

My problem is that i have an arrayList of Squares Each Square has a value(from 0 to 100). Its starting value is 9999 so i can check if its is checked. If a square is checked i want it to be removed from the arrayList. So after a while there will be no Squares left.

there is a little bit of code where the first value is set so thats why i check if the value is 9999.

But i get an error. One that i havent seen before.

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException

Vak = Square

this is my code:

while (!vakken.isEmpty()) { // check if empty
        Iterator itrVak = vakken.iterator();
        while (itrVak.hasNext()) {
            Vak vak = (Vak) itrVak.next(); // here is get the error
            if (vak.getValue() != 9999) {// check if square value is 9999
                Collection checkVakken = vak.getNeighbour().values();
                Iterator itre = checkVakken.iterator();
                while (itre.hasNext()) {
                    Vak nextVak = (Vak) itre.next();
                    if (nextVak != null) {
                        if (nextVak.getValue() == 9999) {
                            nextVak.setValue(vak.getValue() + 1); // set value by its neighbour
                            vakken.remove(vak);
                            checkvakken.add(vak);
                        }
                    }
                }
            } else {
                vakken.remove(vak);
                checkvakken.add(vak);
            }
        }
    }

4 Answers 4

3

You are removing elements from the collection while you are iterating it. As the iterator may produce unpredictable results in this situation, it fails fast throwing the exception you encountered.

You may only alter a collection through the iterator's methods while traversing it. There should be remove method on the iterator itself, that removes the current element and keeps the iterator intact.

Sign up to request clarification or add additional context in comments.

1 Comment

so it should be itrVak.remove();? but when i do that it trows Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException
2

While iterating, you should use Iterator instance for removing object:

itre.remove();

6 Comments

But must be removed from vakken not from the iterator. So if its is an iterator and i remove it there it will also be removed from vakken(arrayList)
@KevinKamer: it will be removed from the original instance.
an iterator doesnt let me remove an object. only remove what he is processing. But it trows Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException
it will remove the current item. use right iterator instance.
@PrinceJohnWesley, I think there is no method like remove() with arguement in Iterator Class.You can call like this: itre.remove();
|
1

You can try like this:

itre.remove();

Comments

0

ITERATOR never lets you modify when you are iterating.. you need to use loops instead.. this happens coz you are using the Iterator, same time other thread is modifying the list...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.