2

I'm trying to iterate an ArrayList with the Iterator. The problem is that I get the ConcurrentModificationException. I have checked a lot of questions like this. All said that the exception occurs when you modify an ArrayList while iterating through it. But I think that my problem is a little bit different because I'm not modifying the ArrayList even with the Iterator methods.

        Cliente cliente1 = new Cliente(16096,"Alberto1","pass",1500.0,false);

    ArrayList<Cliente> miLista = new ArrayList<Cliente>();

    Iterator<Cliente> miIterador = miLista.iterator();

    miLista.add(cliente1);

    System.out.println(miLista.get(0).nombre); //This displays "Alberto1"
    System.out.println(miIterador.hasNext()); //This displays 'true'
    miIterador.next(); //Here I get the exception

I don't know how to solve it. It could be a silly question because actually i'm beginner but I got stuck here.

Thank you.

--Well can't even answer my own question so I edit the question with the answer:

Thank you very much to all of you! You all give me the right answer :D Sorry for the silly question by the way :P

SOLVED

2
  • 1
    Reorder Iterator<Cliente> miIterador = miLista.iterator(); and miLista.add(cliente1); Commented Feb 23, 2014 at 13:41
  • Great! It was that! Thank you very much! Commented Feb 23, 2014 at 14:50

5 Answers 5

4

This line is the culprit. You can't modify a list after getting a iterator on it in any way except through the iterator's own remove or add methods.

miLista.add(cliente1);

The iterators returned by ArrayList 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.

Sign up to request clarification or add additional context in comments.

Comments

3

" But I think that my problem is a little bit different because I'm not modifying the ArrayList even with the Iterator methods."

But you are modifying the list by adding an element :)

    ArrayList<Cliente> miLista = new ArrayList<Cliente>();

    Iterator<Cliente> miIterador = miLista.iterator();

    miLista.add(cliente1); 
    ^^^^^^^^^^^^^^^^^^^^^^ here!

Comments

1

This is the problematic line, you are morfing arraylist by adding object to it. Iterator is made on real array list so it throw exception as fail-fast style.

miLista.add(cliente1);

This is piece of code from arrayList class it is fired when you invoke getNext on iterator.

 final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }

Comments

0

You are modifying list after you produce the Iterator. Just swap lines

Iterator<Cliente> miIterador = miLista.iterator();

miLista.add(cliente1);

and everything will work fine.

Comments

0

One alternative is to use a ListIterator instead and rewind the iterator after you insert the new Client

    Cliente cliente1 = new Cliente(16096,"Alberto1","pass",1500.0,false);

    ArrayList<Cliente> miLista = new ArrayList<Cliente>();

    ListIterator<Cliente> miIterador = miLista.listIterator();

    miIterador.add(cliente1);
    miIterador.previous();

    System.out.println(miLista.get(0).getHombre()); //This displays "Alberto1" << notice you're not using the iterator here
    System.out.println(miIterador.hasNext()); //This displays 'true'
    System.out.println(miIterador.next().getHombre()); //This displays "Albert1" again

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.