File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Removing multiple objects from ArrayList

Lars Filbert

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

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

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:
subject: Removing multiple objects from ArrayList
It's not a secret anymore!