Results 1 to 4 of 4
  1. #1
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default ConcurrentModificationException error in for loop

    Hello all,

    I have an error in my main program so I created a mini one that has the same one.

    I have a class name Dog that implements an interface Animal. By creating a dog object I pass in the constructor its name from the Enum AnimalNames. Everything works as intended.
    I store my dogs in an Arraylist<Animal>. Then I use the for loop to find a dog that has a specific name and remove it from the list.

    Java Code:
    public interface Animal {
    
    	public AnimalName getName();
    }
    Java Code:
    public enum AnimalName {
    
    	WOOF;
    
    }
    Java Code:
    public class Dog implements Animal {
    
    	private AnimalName name;
    
    	public Dog(AnimalName name) {
    		this.name = name;
    	}
    
    	public AnimalName getName() {
    		return this.name;		
    	}
    
    }
    Java Code:
    import java.util.ArrayList;
    
    
    public class Controller {
    
    	private ArrayList<Animal> animals = new ArrayList<>();
    
    	public Controller() {
    
    		animals.add(new Dog(AnimalName.WOOF));
    
    		for(Animal animalTemp : animals)
    			System.out.println(animalTemp.getName());
    
    		System.out.println();
    
    		for(Animal animalTemp : animals) {
    //			if(animalTemp.getName() == AnimalNames.Woof)
    			if(animalTemp.getName().equals(AnimalName.WOOF))
    				animals.remove(animalTemp);
    		}
    
    		System.out.println(animals);
    	}
    
    
    }
    At line 15, it prints WOOF. But it doesn't remove the object. I get this error

    Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unk nown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
    at Controller.<init>(Controller.java:17)
    at Main.main(Main.java:5)

    Controller.java:17 is the line

    Java Code:
    for(Animal animalTemp : animals) {
    which doesn't make sense for me in this specific line.

    The program isn't working either using line 18 or 19.

    My main is a simple new Controller();

    Any help would be appreciated.

    Thank you.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: ConcurrentModificationException error in for loop

    You have to remove the instance outside the loop. You have to remember either the index or the reference to the object you want to remove.
    The Iterable<E> interface is used by the for loop and removing an item from the list would modify the Iterator.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    902
    Rep Power
    2

    Default Re: ConcurrentModificationException error in for loop

    You can also use the Iterator itself to remove the item. This will remove the item from the list via the iterator: "Removes from the underlying collection the last element returned by this iterator"
    Java Code:
            for (Iterator<Animal> iterator = myAnimalList.iterator(); iterator.hasNext(); ) {
                if ( animalNeedsRemoving) {
                    iterator.remove();
                }
            }

  4. #4
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default Re: ConcurrentModificationException error in for loop

    Thank you PhHein's and SurfMan.

    You both gave me knowledge of Iteration interface which I had no idea. It is a very good thing to know and use.

Similar Threads

  1. java.util.ConcurrentModificationException
    By user92 in forum New To Java
    Replies: 2
    Last Post: 11-02-2012, 05:31 PM
  2. java.util.ConcurrentModificationException
    By mharsijamel in forum New To Java
    Replies: 1
    Last Post: 11-02-2011, 02:40 PM
  3. I'm getting a ConcurrentModificationException
    By gib65 in forum Advanced Java
    Replies: 6
    Last Post: 08-18-2010, 09:45 PM
  4. Getting around a ConcurrentModificationException
    By Newbie666 in forum New To Java
    Replies: 50
    Last Post: 01-26-2010, 02:30 PM
  5. ConcurrentModificationException with ArrayList
    By fogus in forum New To Java
    Replies: 3
    Last Post: 01-15-2009, 04:04 AM

Posting Permissions

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