ConcurrentModificationException
全部标签 我正在调用返回TreeMap实例的函数,在调用代码中我想修改TreeMap。但是,我得到了一个ConcurrentModificationException。这是我的代码:publicMapfunction1(){Mapkey_values=Collections.synchronizedMap(newTreeMap());//allkey_values.put()goesherereturnkey_values;}我的调用代码是:Mapkey_values=Collections.synchronizedMap(Classname.function1());//herekey_val
这个问题在这里已经有了答案:ConcurrentModificationExceptiondespiteusingsynchronized(3个答案)关闭6年前。我使用Vector而不是ArrayList来使列表在多线程环境中安全。但是,当我尝试在Vector迭代时将项目添加到Vector时,我不断收到ConcurrentModificationException。为什么会这样?我该如何预防?
以下代码抛出java.util.ConcurrentModificationException,正如预期的那样:publicvoidtest(){ArrayListmyList=newArrayList();myList.add("String1");myList.add("String2");myList.add("String3");myList.add("String4");myList.add("String5");for(Strings:myList){if(s.equals("String2")){myList.remove(s);}}}但是,下面的代码不会抛出异常,而我预
当我遍历下面代码中的LinkedHashMap结构时,不确定是什么触发了java.util.ConcurrentModificationException。使用Map.Entry方法效果很好。没有从以前的帖子中得到关于触发此问题的良好解释。如有任何帮助,我们将不胜感激。importjava.util.LinkedHashMap;importjava.util.Map;publicclassLRU{//privateMapm=newHashMap();//privateSortedMaplru_cache=Collections.synchronizedSortedMap(newTree
嘿SOGuru我正在用这段代码做一份工作publicvoidkill(doubleGrowthRate,intDeath){intbefore=population.size();for(PopulationMemberp:population){int[]probs=ProbablityArrayDeath(GrowthRate,Death,(int)p.fitness());if(probs[RandomNumberGen.nextRandomInt(0,99)]==0){population.remove(p);}}System.out.println("IntialPopula
这个问题在这里已经有了答案:WhyisaConcurrentModificationExceptionthrownandhowtodebugit(8个答案)java.util.ConcurrentModificationExceptionnotthrownwhenexpected(2个答案)关闭3年前。如预期的那样,以下Java代码抛出ConcurrentModificationException:publicclassEvil{publicstaticvoidmain(String[]args){Collectionc=newArrayList();c.add("lalala");c
这个问题在这里已经有了答案:ConcurrentModificationException:addingtoanArrayList(10个答案)关闭7年前。我正在尝试将foreach循环与arraylist一起使用,但是当我使用它时,它给我错误,但是当我使用普通的for循环时,它工作得很好,可能是什么问题?代码在这里:for(Pairp2:R){if((p2.getFirstElm()==p.getSecondElm())&&(p2.getFirstElm()!=p2.getSecondElm()))R.add(newPair(p.getFirstElm(),p2.getSecondE
我的android应用中有以下代码:/***callbackexecutedafterfetchingthedata.*/publicvoidOnPointsFetch(ArrayListresult){toggleLoader(false);this.shops=result;if(activeFilter==Constants.POINTS_FILTER_AVAILABLE){for(Shops:result){if(s.getClientPoints().getPointsAvailable()==0){this.shops.remove(s);}}}else{for(Shops
我想念在Android中同步代码的概念。场景屏幕上总是绘制3个项目。每个图像都存储在一个ArrayList(lstGraphics)中。为此,我使用了SurfaceView。一旦用户点击一张图片,该图片的市场就会被移除,并会添加一个新的市场。代码示例:动画隐藏线程...@Overridepublicvoidrun(){Canvasc;while(run){c=null;try{c=panel.getHolder().lockCanvas(null);synchronized(panel.getHolder()){panel.updatePhysics();panel.manageAni
我有一个包含一些键(字符串)和值(POJO)的映射我想遍历这张map并改变POJO中的一些数据。我继承的当前代码删除了给定的条目,并在对POJO进行了一些更改后将其重新添加。这效果不好,因为您不应该在迭代map时修改map(方法已同步,但ConcurrentModificationException仍然出现)我的问题是,如果我需要遍历map并更改值,我可以使用哪些最佳实践/方法?要创建一个单独的map并在我去的时候构建它,然后返回副本? 最佳答案 两种选择:选项1ThecurrentcodeI'veinheritedremovest