• Post Reply Bookmark Topic Watch Topic
  • New Topic

Removing Items from an Iterated ArrayList  RSS feed

 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try using the remove method of an iterator, it will instead create a duplicate of the next element in the ArrayList.

For example if I have my ArrayList with:
Object A
Object B

and I while the iterator is on Object A, and I use it.remove() I end up with and ArrayList of:
Object B
Object B

Obviously this is not what I want.
Code as follows...

 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

and I while the iterator is on Object A, and I use it.remove() I end up with and ArrayList of:
Object B
Object B

How did you verify this? Did you put something like the following directly after the while loop?



I see some strange things in the code:

Did you deliberately print out the same object each time (get(0))?



This means that in the [A, B] list case, B will be processed inside the while loop and once again in this if statments after the while loop?

To be correct, the behaviour of the remove() method depends on the implementation of the Iterator (and possibly the behaviour of the underlying List), but if it is really an ArrayList, it should give you the expected behaviour.
 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, wow, missing that made me feel a little bit slow.

Yeah, that was my problem, and it really is working as was intended.

Thanks for the help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!