0

When I run the below code, I get an exception and I don't know why.

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
at java.util.ArrayList$Itr.next(ArrayList.java:831)
at simulateur.Simulateur.main(Simulateur.java:218)

Line 218 is for : for(Request r : Sys.queue) and Sys.queue is an ArrayList

    //*************** DispatchRequest ***************
    if(!Sys.queue.isEmpty())
    {
        algo = new SortingAlgo(Sys.queue, clock);
        Sys.queue = algo.sorted_queue;
        for(Request r : Sys.queue)
        {
            {       
                for(Porter p : p_i.porList)
                {

                    if(p.p_state.equals("Available"))
                    {
                        ...
                        ...
                        Sys.queue.remove(r);
                    }
                }
            }
        }
    }
shmosel
  • 49,289
  • 6
  • 73
  • 138
  • 2
    You're removing from the list while iterating over it, that's a terrible idea and is what's causing this exception to be thrown. Research how to properly remove an element from an `ArrayList`. – Jonny Henly Sep 20 '16 at 01:57
  • 1
    Have you read the document about this exception? ArrayList? – xiaofeng.li Sep 20 '16 at 01:57
  • 2
    What does a ConcurrentModificationException mean? (You tell me, since you read the documentation that tells you what it means, right?) – user253751 Sep 20 '16 at 01:59

3 Answers3

1

In this part of your code:

for(Request r : Sys.queue)
{
    {       
        for(Porter p : p_i.porList)
        {

            if(p.p_state.equals("Available"))
            {
                ...
                ...
                Sys.queue.remove(r);
            }
        }
    }
}

Your for loop is iterating over the elements of Sys.queue while at the same time modifying the contents of the variable by invoking Sys.queue.remove(r);

You can fix this by keeping track of elements you want to remove in a separate list/queue and then using the removeAll(...) method of ArrayList

For example:

for(Request r : Sys.queue)
{
    {       
        for(Porter p : p_i.porList)
        {

            if(p.p_state.equals("Available"))
            {
                ...
                ...
                itemsToRemove.add(r);
            }
        }
    }
}
Sys.queue.removeAll(itemsToRemove);
D.B.
  • 4,523
  • 2
  • 19
  • 39
1

You are not allowed to remove from an ArrayList while you're iterating over it. However you can remove directly from the iterator itself using the Iterator.remove() method.

The enhanced for loop that you're using to iterate over Sys.queue doesn't expose the iterator, but if you change it to an old-style for-loop, you can access the remove method:

if(!Sys.queue.isEmpty())
{
    algo = new SortingAlgo(Sys.queue, clock);
    Sys.queue = algo.sorted_queue;
    // For-loop on next line changed to old-style for-loop:
    for (Iterator<Request> iterator = Sys.queue.iterator(); iterator.hasNext();)
    {
        Request r = iterator.next();
        {       
            for(Porter p : p_i.porList)
            {
                if(p.p_state.equals("Available"))
                {
                    iterator.remove(); // The safe way to remove while iterating
                }
            }
        }
    }
}
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
0

Take a look at ConcurrentModificationException for a good explanation on why this exception happens. In you case, you're iterating over a array while trying to change it. As said in javadoc.

The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.

You either want to collect all the elements you want to remove into a list and then do a removeAll() at the very end or put all the elements you don't want to remove into another list & clear this list.

blr
  • 908
  • 4
  • 8