Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.ArrayList$Itr.checkForComodification exception thrown

when running the code below i get the above exception but i don't know why or how to fix it. Im pretty sure it comes from

    for(int node : adjacent(currentnode))
        {
            //System.out.println(adjacent(currentnode));
            //System.out.println(node);
            if (remainingnodes.contains(getNode(node)))
            {
                adjacent.add(node);
                remainingnodes.remove(getNode(node));
                //System.out.println(remainingnodes);
            }
        }

getNode just takes an integer and returns the corresponding node. I used to not get the exception before i used getNode in remainingnodes.contains but at the time it was removing the components so i had to change it and now i get the exception.

public int distance(int target, List<Integer> detectives)
{
    List<Integer> adjacent = new ArrayList<>();
    Set<Node<Integer>> remainingnodes = new HashSet<Node<Integer>>();
    List<Integer> currentnodes = new ArrayList<>();

    int distance = 0;
    int i = 0;



    currentnodes.add(target);
    remainingnodes = graph.getNodes();
    remainingnodes.remove(getNode(target));

    while (detectives.size() != 0)
    {

        for (int currentnode : currentnodes)
        {

            for(int node : adjacent(currentnode))
            {
                //System.out.println(adjacent(currentnode));
                //System.out.println(node);
                if (remainingnodes.contains(getNode(node)))
                {
                    adjacent.add(node);
                    remainingnodes.remove(getNode(node));
                    //System.out.println(remainingnodes);
                }
            }
            for (int detective : detectives)
            {
                if (currentnode == detective)
                {
                    distance = distance + i;
                    detectives.remove(detective);
                }
            }
        }

    currentnodes.clear();
    currentnodes = adjacent;
    i++;
    }

Thanks Arthur

like image 696
Arthur Le Calvez Avatar asked Dec 24 '22 19:12

Arthur Le Calvez


1 Answers

You cant modify the List in for each loop. If you want to remove any elements in loop use iterator. You can remove elements using iterator.remove(); which deletes current element in the iterator.

like image 196
Dilip Kumar Avatar answered Feb 23 '23 11:02

Dilip Kumar