Reputation: 295
I have a question regarding to remove method in arraylist in java, for example:
....
ArrayList<Array>list=new ArrayList<Array>();
Array a=new Array (1,2,3);
Array b=new Array (4,5,6);
Array c=new Array (7,8,9);
list.add(a);
list.add(b);
list.add(c);
....
My question is, if I want to remove object b from the arraylist, should I use list.remove(1) or list.remove(b)? In another way, should I use object or index for the parameter in the remove method in this case?
Upvotes: 0
Views: 418
Reputation: 3543
You can use both, but obviously better will be deleting object, as the order theoretically might change.
EDIT: As @Luiggi Mendoza mentioned - just remember to override equals()
method, if you want to properly use remove(Object o)
. And if you do, don't forget to also override hashCode()
.
Upvotes: 3