| Author |
Problem in arraylist
|
syruss kumar
Ranch Hand
Joined: Jul 23, 2009
Posts: 87
|
|
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: 4166
|
|
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: 213
|
|
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: 19216
|
|
|
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
How To Ask Questions How To Answer Questions
|
 |
syruss kumar
Ranch Hand
Joined: Jul 23, 2009
Posts: 87
|
|
Thanks a lot for the suggestion its helped me to resolve the issue .
|
 |
 |
|
|
subject: Problem in arraylist
|
|
|