Forums Register Login

Problem in arraylist

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
2
+Pie Number of slices to send: Send
 

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:

+Pie Number of slices to send: Send
Both techniques, using an Iterator or looping backwards, will solve this issue. Whichever you prefer is best -- I usually go for the Iterator.
+Pie Number of slices to send: Send
Thanks a lot for the suggestion its helped me to resolve the issue .
please buy this thing and then I get a fat cut of the action:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1176 times.
Similar Threads
ArrayList Problem
ArrayList
Divide a vector
Why I get the List content when I print the List object?
generic question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:12:29.