• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

deleting an object from a list

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I delete an object from an Arraylist. I used the remove(Object obj) method to remove an object from a list and it is returning false. Also I print the size of the list after deleting the object. It is printing the same size before and after the remove method call.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thomas,

Could you provide a bit more information. For instance, what object are you trying to remove?

Does the indexOf(Object) method return -1?
 
Thomas Kundukulam
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. The object is returning -1 for

Book book = (Book)bookQuery.getBooksByBookID(bookIDCheck);
System.out.println("index of object is === "+bookCartList.indexOf(book));
boolean flag = bookCartList.remove(book);
System.out.println(flag);

Where Book is a custom class. flag is getting as false. bookCartList is an ArrayList.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are some possible problems with the remove() method on a List.

If you try to remove an object from a list while you're using an iterator on the list, the remove will fail. Use the remove method of iterator instead. Here's an example:


Another problem that might occur is misunderstanding the remove(Object o) method. When you pass an object to remove() instead of an index, the first object in the list that meets the criteria: listObject.equals(o) will be removed. For most classes you've created, this test will only be true if listObject and o are exactly the same instance of the class. To change this behavior, you must override the equals() method in the class to define equality between two instances of the class.

So, in your example, if you consider two instances of Book to be equal if they have the same ISBN property, you would override the equals(Object o) method that Book inherits from Object so that it returns true if the ISBN properties are the same.
[ April 30, 2006: Message edited by: Merrill Higginson ]
 
Thomas Kundukulam
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My book class contains the instance variables as follows :-

int bookID;
String bookTitle;
String authorName;
String classfication;
int copies;

So how should I override the equals() method?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, this link is a good article explaining all of the intricacies of overriding methods from java.lang.Object. It may be a little more information than you're looking for, though, so here's gist of it.

You decide what makes one book equal to another. If you've decided that as long as the bookID of one book is equal to the bookID of another book, they should be considered equal, then here's your method:


As the article explains, if you override equals, you should also override hashCode() as well.
[ April 30, 2006: Message edited by: Merrill Higginson ]
 
Thomas Kundukulam
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Merrill... Now it's working fine. I have implemented the equals() override as you suggested. Thanks alot
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic