Results 1 to 5 of 5
- 04-30-2011, 05:09 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
[HELP] Removing objects from an ArrayList!
I've had this issue before with other programs, but I've never found a solution.
This is the part of my code where I have an issue. Everything works fine up until I try to remove the bu object from buArray. Even if I replace "buArray.remove(buArray.indexOf(bu));" with a System.out.println("test") test, it works perfectly. But when I do as shown above, I get this:Java Code:for(Bullet bu: buArray){ bu.moveX(); bu.moveY(); if(!bu.getVisible()){ buArray.remove(buArray.indexOf(bu)); } }
I googled the error, but couldn't find anything that I could understand and helps me.Java Code:Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372) at java.util.AbstractList$Itr.next(AbstractList.java:343) at Panel.actionPerformed(Panel.java:68) at javax.swing.Timer.fireActionPerformed(Timer.java:291) at javax.swing.Timer$DoPostEvent.run(Timer.java:221) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:678) at java.awt.EventQueue.access$000(EventQueue.java:86) at java.awt.EventQueue$1.run(EventQueue.java:639) at java.awt.EventQueue$1.run(EventQueue.java:637) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:648) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
- 04-30-2011, 05:26 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I could be wrong and if I am, perhaps I will learn something as well. What happens if you use a standard for loop instead of a for each loop?
Here is the page on that exception: ConcurrentModificationException (Java 2 Platform SE 5.0)
It says it can be caused by a fail fast iterator which I think the for each loop may be using. I could be wrong but this is an easy change to check.
- 04-30-2011, 05:28 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
Whoa, thanks! It worked.
- 04-30-2011, 05:36 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
The for loop "for(Bullet bu: buArray){" walks along the array. And in the course of this you remove part of the array which you are traversing. This is a bit like removing the floor on which you are walking!
Both ArrayList and LinkedList (and maybe others) provide the following caution: "The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future."
The for loop uses such an iterator, hence the exception. What you should do is obtain your own iterator - with the iterator() method of the array - and use it to walk along the array. When you want to remove an element use the iterator's remove() method. (Note that this remove() method is actually optional.)
[Edit] Slow ;(Java Code:import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class RemoveEg { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add(""); list.add("foo"); list.add(""); list.add("bar"); System.out.println(list); // remove the empty strings //for(String str :list) { // if(str.isEmpty()) { // list.remove(str); // } //} Iterator<String> it = list.iterator(); while(it.hasNext()) { String str = it.next(); if(str.isEmpty()) { it.remove(); } } System.out.println(list); } }
- 04-30-2011, 05:54 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Your explanation was good pbrock, mine was more of an assumption that turned out to be correct, yours verified my thoughts and I did learn something, thanks. I always thought for each loop used iterators but wasnt entirely sure.
@ op: please mark your thread solved with the thread tools at the top of the page.Last edited by sunde887; 04-30-2011 at 05:57 AM.
Similar Threads
-
removing repeated entries in arraylist
By ankit1801 in forum New To JavaReplies: 1Last Post: 04-15-2011, 06:34 AM -
Need Help in Removing Objects from list
By pramod_r20 in forum New To JavaReplies: 2Last Post: 12-10-2010, 12:19 PM -
Removing from an ArrayList while looping thru
By pahiker in forum New To JavaReplies: 7Last Post: 06-22-2010, 07:38 AM -
adding or removing an object from an array of objects
By javanew in forum New To JavaReplies: 1Last Post: 05-04-2010, 11:00 AM -
ArrayList with different objects? Help
By xtrmi in forum New To JavaReplies: 4Last Post: 02-27-2009, 08:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks