GeeCON Prague 2014*
The moose likes Java in General and the fly likes Removing multiple objects from ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Removing multiple objects from ArrayList" Watch "Removing multiple objects from ArrayList" New topic
Author

Removing multiple objects from ArrayList

Lars Filbert
Greenhorn

Joined: Aug 26, 2004
Posts: 9
This seems like a simple problem but I am having trouble nonetheless.

So I know the indexes of the items I want to remove but if I put this in a loop and try to use the remove(index) call - that works fine for the first one - but since there was already one removed - now the indexes are screwed up for the following remove calls.

I would also like for the new list ( the one minus the removed lines) to be in the same order as the orginal list - (minus the removed lines, of course)

What is the elegant/easy way to do this? Convert arrayList to another object or what? This has to be a common problem.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

what if you remove the items starting from the back of the list? That way you won't screw up the indexing for the items that you haven't yet removed.

Lars Filbert
Greenhorn

Joined: Aug 26, 2004
Posts: 9
Cool - thanks for the fast, thorough response.

Yes, I actually tried this method, during a coding frenzy - and typical of programming - this particular piece of code must have been working but another method call after that was skewing my result - so I was barking up the wrong tree - wish I had a dollar for every time I have done that.

Thanks Again!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Removing multiple objects from ArrayList