File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Problem in arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem in arraylist" Watch "Problem in arraylist" New topic
Author

Problem in arraylist

syruss kumar
Ranch Hand

Joined: Jul 23, 2009
Posts: 93
Hi all,

I have tried to iterate a list and if the object contains specific character i have to remove the object but it throws concurrentmodificationexception because iterate has been aware of what is modified.so i tried with for loop but its not working properly.



If i execute this objects in the list is not removed completely.I debugged the code the objects are swapped once its removed.


Please suggest me how to avoid concurrentmodificationexception

All search starts with beginner's luck and all search ends with victor's severly tested.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Go through the tutorial.

edit
the objects are swapped once its removed.

It should be rather obvious that if you remove, say, the second element, then the third element takes its place. To remove elements in a for loop, start at the highest numbered element and proceed toward zero.

luck, db
There are no new questions, but there may be new answers.
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Darryl Burke wrote:To remove elements in a for loop, start at the highest numbered element and proceed toward zero.


To remove objects from a list while iterating over it, using an iterator is the safer way I think. Something like:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Both techniques, using an Iterator or looping backwards, will solve this issue. Whichever you prefer is best -- I usually go for the Iterator.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
syruss kumar
Ranch Hand

Joined: Jul 23, 2009
Posts: 93
Thanks a lot for the suggestion its helped me to resolve the issue .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in arraylist
 
Similar Threads
generic question
Why I get the List content when I print the List object?
Divide a vector
ArrayList Problem
ArrayList