【问题标题】:java.util.ConcurrentModificationException after removing an element of a list删除列表元素后的 java.util.ConcurrentModificationException
【发布时间】:2013-10-01 07:38:17
【问题描述】:

ITSPoI allowedPoi = j.next() 行在执行allowedPoIs.remove(k) 后提供以下错误:

java.util.ConcurrentModificationException 在 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; 
    }

【问题讨论】:

  • 通常不允许一个线程在另一个线程对其进行迭代时修改 Collection。
  • @Ashok - 或者当 同一个线程 对其进行迭代时,这是一个更常见的问题(也是这里的问题)。
  • 足以将Array 替换为VectorList myList = Collections.synchronizedList (myList);

标签: java iterator hashmap


【解决方案1】:

循环运行时,您无法从allowedPoIs 中删除元素。将其从iterator 中删除,例如:

j.remove();

改为allowedPoIs.remove(k);

我会用while 写这个方法以使其更清晰:

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; 
}

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 2017-02-04
    • 2021-03-01
    • 1970-01-01
    • 2012-05-08
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多