java - How to avoid the ConcurrentModificationException -
I'm trying to remove items from an array list with the repeater and I'm having It works about 15 to 20 times and then I get an error in every couple's click I'm not sure that throws an error which time ConcurrentModificationException < / Code> Here is my code:
Public Zero Forward (for (Sprites: sprites) {s.move ()); For (Iterator Lt; Sprite & gt; IARR = sprites.iterator (); iter.hasNext ();) {s = iter.next (); If (for example an attacker) {Sprite s2: sprites} {if (s.overlaps (s2)) s2.hit (); }} If (s.shouldRemove ()) iter.remove (); }}}
completed Like exception in the thread "Adbluti -ivent Khuiu -0" java.util.ArrayList $ Itr.next (ArrayList.java:851) java.util on java.util.ArrayList $ Itr.checkForComodification (ArrayList.java:901) on Model.forward. ConcurrentModificationException (Model.java: 46) javax.swing.JComponent.processMouseEvent on java.awt.Component on Controller.mousePressed (Controller.java:29) java.awt.Component.processMouseEvent (Component.java:6522) (JComponent.java : 3321 .processEvent java.awt.Container.processEvent (Container.java:2234) java.awt.Component.dispatchEventImpl (Component.java:4881) on) as the java.awt.Container.dispatchEventImpl At (Component.java:6290) (containers. Java.awt.LightweightDispatcher on Java: 2292) java.awt.Component.dispatchEvent (Component.java:4703) java.awt.LightweightDispatcher.retargetMouseEvent (Container.java:4898) .processMouseEvent (java.awt.Lightwei ghtDispatcher.dispatchEvent (Container.java:4462) java.awt.Container.dispatchEventImpl (Container.java:2278) java.awt.Window.dispatchEventImpl (Window on Container.java:4530). java: 2739) on java.awt.Component.dispatchEvent (component Java: 4703) java.awt.EventQueue.dispatchEventImpl (EventQueue.java:746) java.awt.EventQueue.access $ 400 (on EventQueue.java:97 ) on java.awt.EventQueue $ 3.run (on EventQueue.java: 697) java.awt.EventQueue $ 3.run (EventQueue.java:691) java.security on java.security.ProtectionDomain $ 1.doIntersectionPrivilege (ProtectionDomain .java: 75) on java.security.AccessController.doPrivileged (Native resident ) (ProtectionDomain.java:86) on .ProtectionDomain $ 1.doIntersectionPrivilege java.awt.EventQueue $ 4.run on (EventQueue.java:719) on java.awt.EventQueue $ 4.run (EventQueue.java:717) .doPrivileged (Native method java.awt.EventQueue.dispatchEvent (EventQueue.java:716) on java.awt.EventDispa on java.security.AccessController) java.security.ProtectionDomain $ 1.doIntersectionPrivilege (ProtectionDomain.java:75) On Java.awt.EventDispatchThread.pumpEventsForFilter (Event DispatchThread.java116) on tchThread.pumpOneEventForFilters (Event DispatchThread.java20001) java.awt.EventDispatchThread.pumpEventsForHierarchy (EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents (EventDispatchThread Java : java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java:93) java.awt.EventDispatchThread.run (101) on EventDispatchThread.java:82)
because you have a Removes a ConcurrentModificationException
element from a collection when you're running it on the archive, except for an iterator. In this case, there is no clear-cut erratator for your external repetition, so there is no way to safely modify the sprites
collection inside the loop.
The best you can possibly do is to collect the elements in a temporary collection, then delete them all after the loop, something like this:
Set & lt; Sprite & gt; ToRemove = New Hashet & lt; Sprite & gt; (); (Sprite s1: The sprites) (if (toRemove.contains (s1)) {continue;} S1.move (); (sprites: the sprites) (if (toRemove.contains)) {continue;} if (S. Invasive for example) {for (Sprite s2: sprites) {if (toRemove.contains (s2)) {continue;} if (s.overlaps (s2)) {s2.hit ();}}} .shouldRemove ()) {toRemove.add (s);}}} Sprites.removeAll (toRemove);
Comments
Post a Comment