Caminar una colección

Mucho ojo al tratar de caminar una colección:

ConcurrentModificationException This exception may be thrown by methods that have detected concurrent
modification of an object when such modification is not permissible.

Note that this exception does not always indicate that an object has
been concurrently modified by a /different/ thread. If a single thread
issues a sequence of method invocations that violates the contract of an
object, the object may throw this exception. For example, if a thread
modifies a collection directly while it is iterating over the collection
with a fail-fast iterator, the iterator will thow this exception.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/ConcurrentModificationException.html

Esto es culpa del Iterator a traves del cual recorremos la clase (por mas que usemos un “enhanced for” en el fondo sigue habiendo un iterator) y hay que recordar que no se puede modificar una lista, agregando o eliminando objetos, mientras la estamos recorriendo con un iterator.

No hay comentarios »

RSS feed de los comentarios de este post. TrackBack URI

Comentar

Captcha
Enter the letters you see above.

Entradas y comentarios feeds. 19 queries. 0.323 seconds.

57430 pages viewed, 106 today
27373 visits, 28 today
FireStats icon Powered by FireStats