Results 1 to 5 of 5
  1. #1
    bpx95 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default [HELP] Removing objects from an ArrayList!

    I've had this issue before with other programs, but I've never found a solution.

    Java Code:
    for(Bullet bu: buArray){
    			bu.moveX();
    			bu.moveY();
    			if(!bu.getVisible()){
    				buArray.remove(buArray.indexOf(bu));
    			}
    		}
    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:
    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)
    I googled the error, but couldn't find anything that I could understand and helps me.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    bpx95 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    Whoa, thanks! It worked.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.)

    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);
    	}
    }
    [Edit] Slow ;(

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

  1. removing repeated entries in arraylist
    By ankit1801 in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 06:34 AM
  2. Need Help in Removing Objects from list
    By pramod_r20 in forum New To Java
    Replies: 2
    Last Post: 12-10-2010, 12:19 PM
  3. Removing from an ArrayList while looping thru
    By pahiker in forum New To Java
    Replies: 7
    Last Post: 06-22-2010, 07:38 AM
  4. Replies: 1
    Last Post: 05-04-2010, 11:00 AM
  5. ArrayList with different objects? Help
    By xtrmi in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 08:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •