简体   繁体   English

删除列表元素后的java.util.ConcurrentModificationException

[英]java.util.ConcurrentModificationException after removing an element of a list

The line ITSPoI allowedPoi = j.next() provides the following error after executing allowedPoIs.remove(k) : 在执行allowedPoIs.remove(k)之后,行ITSPoI allowedPoi = j.next()提供了以下错误:

java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) java.util.ArrayList $ Itr.checkForComodification处的java.util.ConcurrentModificationException(未知源)

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

You can't remove element from allowedPoIs when you run in loop. 循环运行时,无法从allowedPoIs删除元素。 Remove it from iterator like: iterator其删除,例如:

j.remove();

instead allowedPoIs.remove(k); 相反, allowedPoIs.remove(k);

I would write this method with while to make it clearer: 我将用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; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用INDEX删除ArrayList中的元素,从而导致java.util.ConcurrentModificationException - Removing element in ArrayList using INDEX causing java.util.ConcurrentModificationException 从HashMap中删除元素时发生异常java.util.ConcurrentModificationException - Exception when removing element from HashMap java.util.ConcurrentModificationException java.util.ConcurrentModificationException但我没有删除 - java.util.ConcurrentModificationException But I am not removing java.util.ConcurrentModificationException不列出删除错误 - java.util.ConcurrentModificationException Not list remove error 修改列表时出现java.util.ConcurrentModificationException - java.util.ConcurrentModificationException when modifying list 数组列表中的java.util.ConcurrentModificationException - java.util.ConcurrentModificationException in Array List JAVA java.util.ConcurrentModificationException - JAVA java.util.ConcurrentModificationException Java:java.util.ConcurrentModificationException - Java: java.util.ConcurrentModificationException 从列表中添加子列表/从列表中删除子列表时出现java.util.ConcurrentModificationException - java.util.ConcurrentModificationException when adding/removing SubLists from/to lists 从哈希图中删除元素时出现 java.util.ConcurrentModificationException - java.util.ConcurrentModificationException when removing elements from a hashmap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM