Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm suffering on this exception. What's the problem on my code? I just want to separate Person's duplicate name in another ArrayList

public class GlennTestMain
{

    static ArrayList<Person> ps;

    static ArrayList<Person> duplicates;
    public static void main(String[] args)
    {
        ps = new ArrayList<GlennTestMain.Person>();

        duplicates = new ArrayList<GlennTestMain.Person>();

        noDuplicate(new Person("Glenn", 123));
        noDuplicate(new Person("Glenn", 423));
        noDuplicate(new Person("Joe", 1423)); // error here


        System.out.println(ps.size());
        System.out.println(duplicates.size());
    }

    public static void noDuplicate(Person p1)
    {
        if(ps.size() != 0)
        {
            for(Person p : ps)
            {
                if(p.name.equals(p1.name))
                {
                    duplicates.add(p1);
                }
                else
                {
                    ps.add(p1);
                }
            }
        }
        else
        {
            ps.add(p1);
        }
    }

    static class Person
    {
        public Person(String n, int num)
        {
            this.name = n;
            this.age = num;
        }
        String name;
        int age;
    }



}

Here's the stacktrace

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at hk.com.GlennTestMain.noDuplicate(GlennTestMain.java:41)
at hk.com.GlennTestMain.main(GlennTestMain.java:30)
share|improve this question
1  
What is the exception? –  The Unusual Oct 30 '12 at 6:22
 
+1 Thanks for detailed answers. It really helped –  user948620 Oct 30 '12 at 6:37
 
possible duplicate of concurrentModificationException –  Kris Oct 30 '12 at 11:20

3 Answers

up vote 4 down vote accepted

You cannot modify the collection you are iterating on. That might throw a ConcurrentModificationException. Though it might work sometimes, but it is not guaranteed to work everytime.

If you want to add, or remove something from your list, you need to use an Iterator, or ListIterator for your list. And use ListIterator#add method to add anything in your list. Even if in your iterator, if you try to use List.add or List.remove, you will get that exception, because that doesn't make any difference. You should use the methods of iterator.

See these posts to understand how to use it: -

share|improve this answer

Acc. to Java Docs:

if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.

You are tyring to add a Person object while iterating it using Enhanced For loop.

You can do following modification:

boolean duplicateFound = false;
for(Person p : ps)
{
    if(p.name.equals(p1.name))
    {
        duplicates.add(p1);
        duplicateFound = true;
    }
}

if( ! duplicateFound)
{
    ps.add(p1);
}
share|improve this answer

Reason?

Iterators returned by ArrayList is fail-fast in nature.

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.

Where does this iterator Come from while I am not using it?

For enhanced for loop for collections Iterator gets used so you can not call add method while you are iterating.

So your loop is same as below

for (Iterator<Entry> i = c.iterator(); i.hasNext(); ){   

What is the Solution Then ?

You can call iterator.add(); and change loop based on iterator explicitly rather than implicitly.

    String inputWord = "john";
    ArrayList<String> wordlist = new ArrayList<String>();
    wordlist.add("rambo");
    wordlist.add("john");
    for (ListIterator<String> iterator = wordlist.listIterator(); iterator
            .hasNext();) {
        String z = iterator.next();
        if (z.equals(inputWord)) {
            iterator.add("3");
        }
    }
    System.out.println(wordlist.size());

Now Where Can I Read more?

  1. The For-Each Loop
  2. ArrayList Java docs
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.