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
Iterator<Cliente> miIterador = miLista.iterator();andmiLista.add(cliente1);