concurrentmodification

How can I fix this error java.util.ConcurrentModificationException

时光毁灭记忆、已成空白 提交于 2019-11-28 00:16:04
I get an error on the following line. I'm doing the process of adding to the jsonarray. Please help me. jsonArr=new JSONArray(); if(req.getSession().getAttribute("userses")!=null){ String name=(req.getParameter("name")==null?"":to_EnglishName(req.getParameter("name").toUpperCase())); if(!name.equals("")){ for(Book c:GlobalObjects.bookList){ if(c.getBookName().startsWith(name)){ jsonObjec=new JSONObject(); jsonObjec.put("label",c.getBookName()); jsonObjec.put("value", c.getId()); jsonArr.add(jsonObjec);//java.util.ConcurrentModificationException } } } } jsonArr.write(res.getWriter()); This is

java.util.ConcurrentModificationException in Android animation

狂风中的少年 提交于 2019-11-27 20:57:20
问题 There is something I miss with the notion of Synchronizing code in Android. Scenario There are always 3 items drawn on the screen. Each image is stored in a ArrayList (lstGraphics). For this purpose I use a SurfaceView. Once the user taps on a image, the image get's market to be removed and a new one will be added. Code samples: AnimationHideThread ... @Override public void run() { Canvas c; while (run) { c = null; try { c = panel.getHolder().lockCanvas(null); synchronized (panel.getHolder())

Using iterator on a TreeSet

夙愿已清 提交于 2019-11-27 18:03:44
问题 SITUATION: I have a TreeSet of custom Objects and I have also used a custom Comparator. I have created an iterator to use on this TreeSet. TreeSet<Custom> ts=new TreeSet<Custom>(); Iterator<Custom> itr=ts.iterator(); while(itr.hasNext()){ Custom c=itr.next(); //Code to add a new element to the TreeSet ts } QUESTION: Well I want to know that if I add a new element to the TreeSet within the while loop, then will that new element get sorted immediately. In other words, if I add a new element

Why does one loop throw a ConcurrentModificationException, while the other doesn't?

自闭症网瘾萝莉.ら 提交于 2019-11-27 18:01:58
问题 I've run into this while writing a Traveling Salesman program. For an inner loop, I tried a for(Point x:ArrayList<Point>) { // modify the iterator } but when adding another point to that list resulted in a ConcurrentModicationException being thrown. However, when I changed the loop to for(int x=0; x<ArrayList<Point>.size(); x++) { // modify the array } the loop ran fine without throwing an exception. Both a for loops, so why does one throw an exception while the other does not? 回答1: As others

How to deal with ConcurrentModificationException

强颜欢笑 提交于 2019-11-27 08:25:04
问题 I am getting the a ConcurrentModificationException from my cooldown timer. I use a thread to reduce the values every second like this: public class CoolDownTimer implements Runnable { @Override public void run() { for (String s : playerCooldowns.keySet()) { playerCooldowns.put(s, playerCooldowns.get(s) - 20); if (playerCooldowns.get(s) <= 0) { playerCooldowns.remove(s); } } } } So every second it should reduce every players cooldown by 20, but the problem is that I a getting the CME every

Collection throws or doesn't throw ConcurrentModificationException based on the contents of the Collection [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-11-27 02:04:01
This question already has an answer here: Why is a ConcurrentModificationException thrown and how to debug it 6 answers The following Java code throws a ConcurrentModificationException , as expected: public class Evil { public static void main(String[] args) { Collection<String> c = new ArrayList<String>(); c.add("lalala"); c.add("sososo"); c.add("ahaaha"); removeLalala(c); System.err.println(c); } private static void removeLalala(Collection<String> c) { for (Iterator<String> i = c.iterator(); i.hasNext();) { String s = i.next(); if(s.equals("lalala")) { c.remove(s); } } } } But the following

How can I fix this error java.util.ConcurrentModificationException

随声附和 提交于 2019-11-26 23:24:08
问题 I get an error on the following line. I'm doing the process of adding to the jsonarray. Please help me. jsonArr=new JSONArray(); if(req.getSession().getAttribute("userses")!=null){ String name=(req.getParameter("name")==null?"":to_EnglishName(req.getParameter("name").toUpperCase())); if(!name.equals("")){ for(Book c:GlobalObjects.bookList){ if(c.getBookName().startsWith(name)){ jsonObjec=new JSONObject(); jsonObjec.put("label",c.getBookName()); jsonObjec.put("value", c.getId()); jsonArr.add

Why isn't this code causing a ConcurrentModificationException? [duplicate]

痴心易碎 提交于 2019-11-26 19:08:29
This question already has an answer here: Why is a ConcurrentModificationException thrown and how to debug it 6 answers I was reading about ConcurrentModificationException and how to avoid it. Found an article . The first listing in that article had code similar to the following, which would apparently cause the exception: List<String> myList = new ArrayList<String>(); myList.add("January"); myList.add("February"); myList.add("March"); Iterator<String> it = myList.iterator(); while(it.hasNext()) { String item = it.next(); if("February".equals(item)) { myList.remove(item); } } for (String item

How to modify a Collection while iterating using for-each loop without ConcurrentModificationException? [duplicate]

霸气de小男生 提交于 2019-11-26 18:58:31
This question already has an answer here: Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop 23 answers If I modify a Collection while iterating over it using for-each loop, it gives ConcurrentModificationException . Is there any workaround? Use Iterator#remove . This is the only safe way to modify a collection during iteration. For more information, see The Collection Interface tutorial. If you also need the ability to add elements while iterating, use a ListIterator . One work around is to save your changes and add/remove them after the

Collection throws or doesn&#39;t throw ConcurrentModificationException based on the contents of the Collection [duplicate]

亡梦爱人 提交于 2019-11-26 09:53:31
问题 This question already has an answer here: Why is a ConcurrentModificationException thrown and how to debug it 7 answers The following Java code throws a ConcurrentModificationException , as expected: public class Evil { public static void main(String[] args) { Collection<String> c = new ArrayList<String>(); c.add(\"lalala\"); c.add(\"sososo\"); c.add(\"ahaaha\"); removeLalala(c); System.err.println(c); } private static void removeLalala(Collection<String> c) { for (Iterator<String> i = c