• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to delete from List without distutbing the position of other objects.  RSS feed

 
sujai chellappa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a List from which i want to delete objects based on there position.

When i delete an Object from the list from say index :4 ,the list gets sorted and all the objects from below are shifted to upper index..
I mean i want the object to be deleted from the mentioned index ,but the shifting should not happen.

Is there any way to do this.

Please Help

Thanks
Sujai.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you delete item number 4 from a list, the old number 5 is moved down to become the new number 4. That's the definition of deleting an entry from the list.

If you'd like the list to contain a null (or an empty String, or whatever) at that position instead, then don't delete the item; just set that element to the contents you want -- i.e.,

theList.set(4, null);
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!