Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing multiple objects from ArrayList

 
Lars Filbert
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic