ConcurrentModificationException thrown by crop(x) and a.setFrom()

28 views Asked by At

I am current lost in that problem. I have read a lot here about other problems with ConcurrentModificationException, but can't resolve my problem. Maybe i don't see the wood for the trees. I hope you can help me. The method is

public void crop(PesNode n) {
    for (int ii = n.getPostNodes().size()-1; ii >= 0; ii--) {
        for (int i = ii-1; i >= 0; i--) {
            PesNode n1=n.getPostNodes().get(i);
            PesNode n2=n.getPostNodes().get(ii);
            if (n1.getName().equals(n2.getName())) {
                
                boolean merge=true;
                for (PesNode node : n1.getPreNodes()) {
                    if (!n2.getPreNodes().contains(node)) {
                        merge=false;
                    }
                }
                for (PesNode node : n2.getPreNodes()) {
                    if (!n1.getPreNodes().contains(node)) {
                        merge=false;
                    }
                }
                
                if (merge) {
                    //Merge n1 and n2
                    
                    //for(Iterator<PesArc> itPesArc = n2.getPost().iterator(); itPesArc.hasNext();) { PesArc a = itPesArc.next();
                    for(PesArc a : n2.getPost()) {
                        a.setFrom(n1);
                        n1.getPost().add(a);
                        
                    }
                    //for(Iterator<PesArc> itPesArc = n2.getPost().iterator(); itPesArc.hasNext();) { PesArc a = itPesArc.next();
                    for(PesArc a : n2.getPre()) {
                        arcs.remove(a);
                        a.getFrom().getPost().remove(a);
                    }
                    //for(Iterator<Integer> it = n2.getCoset().iterator(); it.hasNext();) { int j = it.next();
                    for (int j : n2.getCoset()) {
                        if (!n1.getCoset().contains(j)) {
                            n1.getCoset().add(j);
                        }
                    }

                    nodes.remove(n2);
                    
                    //n2.getPost().removeIf(Objects::isNull);
                    //n2.getPre().removeIf(Objects::isNull);
                    //n2.getCoset().removeIf(Objects::isNull);
                    i=0;
                }

            }

        }

    }

    for (PesNode x : n.getPostNodes()) {
        crop(x);
    }

The errors comes from the "crop(x)" at the end and pops up at "a.setFrom(n1);" I dont understand why this happens, because every for loop comes to its ends before the next one starts. I also tried to use iterators instead of the for loops, without any solution. What do I wrong?

1

There are 1 answers

1
Level_Up On

You get ConcurrentModificationException because you try to remove element while you are using for operator. So I think the line in your code nodes.remove(n2); can lead to this exception. You can use Iterator or just to gather the elements for removal in another Collection and then remove it. For more information: https://www.baeldung.com/java-concurrentmodificationexception