0

The line ITSPoI allowedPoi = j.next() provides the following error after executing allowedPoIs.remove(k):

java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source)

private Map<String,Integer> findClosest(Route tempRoute, List<ITSPoI> allowedPoIs, List<ITSPoI> tabulist) {  
    double cost,mincost = 999999999; 
    Map<String,Integer> closest = new HashMap<String,Integer>();

    for (int i=0; i<tempRoute.getPOIs().size(); i++)        {
        int k = 0;
        for(Iterator<ITSPoI> j = allowedPoIs.iterator(); j.hasNext(); )  {

            ITSPoI allowedPoi = j.next();
            if (!intabu(allowedPoi,tabulist))
            {
                try 
                {
                    cost = _cvrtw.getCostMatrix().getCost(tempRoute.getPOI(i).getNodeId(),allowedPoi.getNodeId());

                    if (cost<mincost){
                        mincost = cost;
                        closest.put("index",i);
                        closest.put("poi",k);
                        allowedPoIs.remove(k);

                    }
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
            k++; 
            } 
        }
    return closest; 
    }
3
  • it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. Commented Oct 1, 2013 at 7:41
  • @Ashok - or while the same thread is iterating over it, which is a more common problem (and the issue here). Commented Oct 1, 2013 at 7:46
  • enough to replace Array with Vector or List myList = Collections.synchronizedList (myList); Commented Oct 1, 2013 at 7:52

1 Answer 1

4

You can't remove element from allowedPoIs when you run in loop. Remove it from iterator like:

j.remove();

instead allowedPoIs.remove(k);

I would write this method with while to make it clearer:

private Map<String,Integer> findClosest(Route tempRoute, List<ITSPoI> allowedPoIs, List<ITSPoI> tabulist) {  
    double cost,mincost = 999999999; 
    Map<String,Integer> closest = new HashMap<String,Integer>();

           Iterator<ITSPoI> iter;

    for (int i=0; i<tempRoute.getPOIs().size(); i++)        {
        int k = 0;

        iter = allowedPoIs.iterator();

        while(iter.hasNext()){
            ITSPoI allowedPoi = iter.next();
            if (!intabu(allowedPoi,tabulist))
            {
                try 
                {
                    cost = _cvrtw.getCostMatrix().getCost(tempRoute.getPOI(i).getNodeId(),allowedPoi.getNodeId());

                    if (cost<mincost){
                        mincost = cost;
                        closest.put("index",i);
                        closest.put("poi",k);                           
                        iter.remove(); // fix
                    }
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
            k++; 
        }       

    }
    return closest; 
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.