Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How do you iterate over a List and remove elements?

Reply
Thread Tools

How do you iterate over a List and remove elements?

 
 
Knute Johnson
Guest
Posts: n/a
 
      08-24-2007
I've got an ArrayList and I want to iterate over the list and remove the
occasional element. If I use the new for loop it throws a
ConcurrentModificationException. An Iterator is going to have the same
problem. There must be some simple way to do this that I'm not thinking
of tonight.

Thanks,

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      08-24-2007
Knute Johnson wrote:
> I've got an ArrayList and I want to iterate over the list and remove the
> occasional element. If I use the new for loop it throws a
> ConcurrentModificationException. An Iterator is going to have the same
> problem. There must be some simple way to do this that I'm not thinking
> of tonight.


Use an explicit Iterator-based loop, and the Iterator's remove method,
not the underlying collection's remove.

Patricia
 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      08-24-2007
Knute Johnson wrote:
> I've got an ArrayList and I want to iterate over the list and remove
> the occasional element. If I use the new for loop it throws a
> ConcurrentModificationException. An Iterator is going to have the
> same problem.


The ConcurrentModificationException occurs because you're modifying the List
behind the Iterator's back, so to speak. If you use the Iterator to do the
removes you'll be fine.


 
Reply With Quote
 
Ishwor Gurung
Guest
Posts: n/a
 
      08-24-2007
Knute Johnson wrote:

> I've got an ArrayList and I want to iterate over the list and remove the
> occasional element. If I use the new for loop it throws a
> ConcurrentModificationException. An Iterator is going to have the same


The API docs say that ConcurrentModificationException is thrown when you try
to delete an elment in the array list while the iterator is busy traversing
the elements inside it.

> problem. There must be some simple way to do this that I'm not thinking
> of tonight.


Yes indeed there is. One way to do it is if you can find an element that
you'd like to delete then check it first, delete it and then break out.
Iterator i = l.iterator();
while(i.hasNext())
{
if (i.next().equals("a"))
{
//the key point is breaking out here
l.remove("a");break;
}
}

If you want to delete the whole list, why traverse it in the first place?
hth

--
Cheers,
Ishwor Gurung
/* humpty dumpty */

 
Reply With Quote
 
bugbear
Guest
Posts: n/a
 
      08-24-2007
Ishwor Gurung wrote:
> Knute Johnson wrote:
>
>> I've got an ArrayList and I want to iterate over the list and remove the
>> occasional element. If I use the new for loop it throws a
>> ConcurrentModificationException. An Iterator is going to have the same

>
> The API docs say that ConcurrentModificationException is thrown when you try
> to delete an elment in the array list while the iterator is busy traversing
> the elements inside it.
>
>> problem. There must be some simple way to do this that I'm not thinking
>> of tonight.

>
> Yes indeed there is. One way to do it is if you can find an element that
> you'd like to delete then check it first, delete it and then break out.


The ConcurrentModificationException is caused by the Iterator
"noticing" that you've changed something under its feet.

The proper solution is to "play nice" with the iterator,
and remove the element AND keep the iterator informed.

http://java.sun.com/j2se/1.4.2/docs/...r.html#remove()

BugBear
 
Reply With Quote
 
Frederick Polgardy
Guest
Posts: n/a
 
      08-24-2007
On Aug 24, 1:39 am, Patricia Shanahan <(E-Mail Removed)> wrote:
> Knute Johnson wrote:
> > I've got an ArrayList and I want to iterate over the list and remove the
> > occasional element. If I use the new for loop it throws a
> > ConcurrentModificationException. An Iterator is going to have the same
> > problem. There must be some simple way to do this that I'm not thinking
> > of tonight.

>
> Use an explicit Iterator-based loop, and the Iterator's remove method,
> not the underlying collection's remove.
>
> Patricia


In other words:

Iterator i = l.iterator();
while (i.hasNext()) {
if (shouldRemove(i.next()) {
i.remove();
}
}

You can remove as many elements as you need to in the same iteration.

Fred

 
Reply With Quote
 
Manish Pandit
Guest
Posts: n/a
 
      08-24-2007
On Aug 23, 11:13 pm, Knute Johnson <(E-Mail Removed)>
wrote:
> I've got an ArrayList and I want to iterate over the list and remove the
> occasional element. If I use the new for loop it throws a
> ConcurrentModificationException. An Iterator is going to have the same
> problem. There must be some simple way to do this that I'm not thinking
> of tonight.
>
> Thanks,
>
> --
>
> Knute Johnson
> email s/nospam/knute/


If you are removing one occassional element, you can just remove it
without iterating (or may be I did not get the situation). Something
like:

synchornize(list){
if(list.contains(obj){
list.remove(obj);
}
}

-cheers,
Manish

 
Reply With Quote
 
Manish Pandit
Guest
Posts: n/a
 
      08-24-2007
On Aug 24, 7:09 am, Manish Pandit <(E-Mail Removed)> wrote:

> synchornize(list){


oops..synchronized(list){

 
Reply With Quote
 
Ben Phillips
Guest
Posts: n/a
 
      08-24-2007
Manish Pandit wrote:
> if(list.contains(obj){
> list.remove(obj);
> }


Collections let you do coll.remove(obj) whether or not it's definitely
in there to remove, and return a boolean in case you want to do
something different depending on whether it was there to remove or it
wasn't. This is more efficient as the linked list, array, hash table,
tree, or other backing data structure gets searched only once instead of
twice.
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      08-24-2007
Manish Pandit wrote:
> On Aug 23, 11:13 pm, Knute Johnson <(E-Mail Removed)>
> wrote:
>> I've got an ArrayList and I want to iterate over the list and remove the
>> occasional element. If I use the new for loop it throws a
>> ConcurrentModificationException. An Iterator is going to have the same
>> problem. There must be some simple way to do this that I'm not thinking
>> of tonight.
>>
>> Thanks,
>>
>> --
>>
>> Knute Johnson
>> email s/nospam/knute/

>
> If you are removing one occassional element, you can just remove it
> without iterating (or may be I did not get the situation). Something
> like:
>
> synchornize(list){
> if(list.contains(obj){
> list.remove(obj);
> }
> }


That technique works if you want to remove an item with a specified
value. Suppose, for example, you want to remove every string shorter
than 3 characters from a List<String>. It can be done easily using the
Iterator remove method.

What is the purpose of the list.contains(obj) pre-check?

Patricia
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to iterate 2 nested collections w <logic:iterate> without a"getter" John Java 4 04-01-2008 09:46 AM
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
nested:iterate or logic: iterate with multibox?? runescience Java 0 02-09-2006 12:57 AM
<logic:iterate /> iterate beyond items in the collection Gogo Java 1 09-04-2003 08:40 PM



Advertisments