Reputation: 763
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
Upvotes: 2
Views: 105
Reputation: 12421
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
.
Upvotes: 4
Reputation: 6570
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
Upvotes: 0
Reputation: 2826
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.
Upvotes: 0
Reputation: 3684
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();
}
Upvotes: 1
Reputation: 6452
" 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!
Upvotes: 3