Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Removing object from arraylist when pointed to by iterator

Reply
Thread Tools

Removing object from arraylist when pointed to by iterator

 
 
nooneinparticular314159@yahoo.com
Guest
Posts: n/a
 
      08-24-2008
I have an arraylist of objects of a certain type. I use an iterator
on that arraylist to get the next instance of the object, using the
iterator.next() method. But if I succeed in performing an operation
on the object, I want to remove the object from my arraylist. The
question is how I remove it without calling .next() again, since that
will remove the next one, not the current one? ie. If I am currently
working on the object at position 4 in the arraylist (through the
iterator), I want to remove the object at position 4. But I don't
know what position the object is in because I got it through the
iterator.

Thanks!
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      08-24-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have an arraylist of objects of a certain type. I use an iterator
> on that arraylist to get the next instance of the object, using the
> iterator.next() method. But if I succeed in performing an operation
> on the object, I want to remove the object from my arraylist. The
> question is how I remove it without calling .next() again, since that
> will remove the next one, not the current one? ie. If I am currently
> working on the object at position 4 in the arraylist (through the
> iterator), I want to remove the object at position 4. But I don't
> know what position the object is in because I got it through the
> iterator.


Save a ref to what next() returns.

And I am pretty sure that the ArrayList iterator does not like
you calling remove while iterating.

Arne
 
Reply With Quote
 
 
 
 
softwarepearls_com
Guest
Posts: n/a
 
      08-24-2008
On Aug 24, 3:00*am, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> I have an arraylist of objects of a certain type. *I use an iterator
> on that arraylist to get the next instance of the object, using the
> iterator.next() method. *But if I succeed in performing an operation
> on the object, I want to remove the object from my arraylist. *The
> question is how I remove it without calling .next() again, since that
> will remove the next one, not the current one? *ie. *If I am currently
> working on the object at position 4 in the arraylist (through the
> iterator), I want to remove the object at position 4. *But I don't
> know what position the object is in because I got it through the
> iterator.


I would say you're caught in the iterator "routine" so many fall prey
to... you should not be using an iterator when working with an
ArrayList ! You should be iterating the "old fashioned" way ... using
direct get(int) .. these don't entertain the concept of
ConcurrentModificationException. It's faster too.
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      08-24-2008
(E-Mail Removed) wrote:
> I have an arraylist of objects of a certain type. I use an iterator
> on that arraylist to get the next instance of the object, using the
> iterator.next() method. But if I succeed in performing an operation
> on the object, I want to remove the object from my arraylist. The
> question is how I remove it without calling .next() again, since
> that
> will remove the next one, not the current one? ie. If I am
> currently
> working on the object at position 4 in the arraylist (through the
> iterator), I want to remove the object at position 4. But I don't
> know what position the object is in because I got it through the
> iterator.



If you call remove() on the iterator, you'll remove the last one
fetched. That is, code like

ArrayList<MyType> list;
for (Iterator<MyType> iter = list.iterator(); iter.hasNext(); )
{
MyType obj = list.next();
if (shouldBeRemoved(obj))
iter.remove();
}

does what you want.


 
Reply With Quote
 
Donkey Hottie
Guest
Posts: n/a
 
      08-24-2008
softwarepearls_com <(E-Mail Removed)> wrote in news:f472d0ee-f85b-
(E-Mail Removed):

> On Aug 24, 3:00*am, "(E-Mail Removed)"
><(E-Mail Removed)> wrote:
>> I have an arraylist of objects of a certain type. *I use an iterator
>> on that arraylist to get the next instance of the object, using the
>> iterator.next() method. *But if I succeed in performing an operation
>> on the object, I want to remove the object from my arraylist. *The
>> question is how I remove it without calling .next() again, since that
>> will remove the next one, not the current one? *ie. *If I am currentl

> y
>> working on the object at position 4 in the arraylist (through the
>> iterator), I want to remove the object at position 4. *But I don't
>> know what position the object is in because I got it through the
>> iterator.

>
> I would say you're caught in the iterator "routine" so many fall prey
> to... you should not be using an iterator when working with an
> ArrayList ! You should be iterating the "old fashioned" way ... using
> direct get(int) .. these don't entertain the concept of
> ConcurrentModificationException. It's faster too.


If one wants to iterate thru the ArrayList, and remove items here and
there, I'd say an Iterator is much simpler and safer to use.

Iterating with an index may cause undesired results, as shown by the
following example.

package itertest;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

public class Main
{
public static void main(String[] args)
{
final int SIZE=1000;
List list = new ArrayList(SIZE) ;

for (int i=0; i < SIZE; i++)
{
list.add(Integer.toString(i));
}

for (int i=0; i < list.size(); i++)
{
String s = (String) list.get(i) ;
System.out.println("list.get(" + i + ") (list.size=" +
list.size() + ") = " + s) ;
list.remove(i) ;
}

list = new ArrayList(SIZE) ;

for (int i=0; i < SIZE; i++)
{
list.add(Integer.toString(i));
}

for (Iterator i=list.iterator(); i.hasNext(); )
{
String s = (String) i.next() ;
System.out.println("i.next() = " + s) ;
i.remove();
}
}
}


The list will never get iterated thru by using get() ! The algorighm
using that requires something more, which makes it more complicated.




 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      08-24-2008
(E-Mail Removed) wrote:
> I have an arraylist of objects of a certain type. I use an iterator
> on that arraylist to get the next instance of the object, using the
> iterator.next() method. But if I succeed in performing an operation
> on the object, I want to remove the object from my arraylist. The
> question is how I remove it without calling .next() again, since that
> will remove the next one, not the current one? ie. If I am currently
> working on the object at position 4 in the arraylist (through the
> iterator), I want to remove the object at position 4. But I don't
> know what position the object is in because I got it through the
> iterator.
>
> Thanks!

it.remove(); will remove the object that was last returned by it.next()

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      08-24-2008
softwarepearls_com wrote:
> On Aug 24, 3:00 am, "(E-Mail Removed)"
> <(E-Mail Removed)> wrote:
>> I have an arraylist of objects of a certain type. I use an iterator
>> on that arraylist to get the next instance of the object, using the
>> iterator.next() method. But if I succeed in performing an operation
>> on the object, I want to remove the object from my arraylist. The
>> question is how I remove it without calling .next() again, since that
>> will remove the next one, not the current one? ie. If I am currently
>> working on the object at position 4 in the arraylist (through the
>> iterator), I want to remove the object at position 4. But I don't
>> know what position the object is in because I got it through the
>> iterator.

>
> I would say you're caught in the iterator "routine" so many fall prey
> to... you should not be using an iterator when working with an
> ArrayList ! You should be iterating the "old fashioned" way ... using
> direct get(int) .. these don't entertain the concept of
> ConcurrentModificationException. It's faster too.

Lies!

You don't see the concurrent modification errors, but they still can
cause bugs in your code!
Also, It isn't necessarily faster. I would assume its "about the same"
for the average program, but the benefits of using an iterator far
outweigh the cost for the average program. Fewer bugs, bugs caught
sooner, etc...
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      08-24-2008
Daniel Pitts wrote:
> softwarepearls_com wrote:
>> On Aug 24, 3:00 am, "(E-Mail Removed)"
>> <(E-Mail Removed)> wrote:
>>> I have an arraylist of objects of a certain type. I use an
>>> iterator
>>> on that arraylist to get the next instance of the object, using
>>> the
>>> iterator.next() method. But if I succeed in performing an
>>> operation
>>> on the object, I want to remove the object from my arraylist. The
>>> question is how I remove it without calling .next() again, since
>>> that will remove the next one, not the current one? ie. If I am
>>> currently working on the object at position 4 in the arraylist
>>> (through the iterator), I want to remove the object at position 4.
>>> But I don't know what position the object is in because I got it
>>> through the iterator.

>>
>> I would say you're caught in the iterator "routine" so many fall
>> prey
>> to... you should not be using an iterator when working with an
>> ArrayList ! You should be iterating the "old fashioned" way ...
>> using
>> direct get(int) .. these don't entertain the concept of
>> ConcurrentModificationException. It's faster too.

> Lies!
>
> You don't see the concurrent modification errors, but they still can
> cause bugs in your code!
> Also, It isn't necessarily faster. I would assume its "about the
> same" for the average program, but the benefits of using an iterator
> far outweigh the cost for the average program. Fewer bugs, bugs
> caught sooner, etc...


And it's easier to get right. Iterator.remove() does what the OP
wants. The equivalent with integer indices is:

for (int i = 0; i < list.length(); i++)
{
if (shouldRemove(list.get(i))
{
list.remove(i);
i--; // Needed to avoid skipping a list member.
// Or is it?
}
}

And, when iterating the other way:

for (int i = list.length()-1; i >= 0; i--)
{
if (shouldRemove(list.get(i))
{
list.remove(i);
i++; // Needed to avoid skipping a list member.
// Or is it?
}
}

The answers are "yes" and "no" respectively, but why take the chance
of getting it wrong?


 
Reply With Quote
 
jolz
Guest
Posts: n/a
 
      08-24-2008
Instead of calling iterator.remove() for every object you remove you can:
- remember all object that you want to keep in a temporary list
- after loop is finished clear original list and insert to it content of
the temporary list (or switch references if it isn't used anywherere
else in the code)
Above makes no sense if you have 5 objects in the list, but with many
objects often removed from an ArrayList you will notice that calling
iterator.remove() slows down your application.
Or you can use LinkedList instead of ArrayList if you allways use
iterators - obiously code with a single call iterator.remove(); is
simpler and removing from LinkedList is way faster thar removing from
ArrayList.
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      08-24-2008
On Sun, 24 Aug 2008, softwarepearls_com wrote:

> On Aug 24, 3:00*am, "(E-Mail Removed)"
> <(E-Mail Removed)> wrote:
>
>> I have an arraylist of objects of a certain type. *I use an iterator on
>> that arraylist to get the next instance of the object, using the
>> iterator.next() method. *But if I succeed in performing an operation on
>> the object, I want to remove the object from my arraylist. *The
>> question is how I remove it without calling .next() again, since that
>> will remove the next one, not the current one? *ie. *If I am currently
>> working on the object at position 4 in the arraylist (through the
>> iterator), I want to remove the object at position 4. *But I don't know
>> what position the object is in because I got it through the iterator.

>
> I would say you're caught in the iterator "routine" so many fall prey
> to... you should not be using an iterator when working with an ArrayList
> ! You should be iterating the "old fashioned" way ... using direct
> get(int) .. these don't entertain the concept of
> ConcurrentModificationException. It's faster too.


No. I mean, this is just really terrible advice. Please don't ever give
anyone advice again. You're really not helping.

tom

--
In Milan, [traffic lights] are instructions, in Rome suggestions, and
in Naples Christmas decorations. -- James Dowden
 
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
Store Address of object pointed to nicollete C Programming 6 11-11-2009 06:58 PM
Modify pointed object method behaviour Arkaitz Jimenez C++ 1 06-11-2009 08:41 AM
removing elements invalidates only those iterators that had specifically pointed at the removed elements Alien C++ 6 09-21-2006 03:13 PM
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList leal ting ASP .Net 1 02-10-2004 07:45 PM
Iterate through ArrayList using an another ArrayList Saravanan Rathinavelu ASP .Net 3 08-19-2003 07:03 AM



Advertisments