0

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?

pei wang
  • 295
  • 2
  • 8
  • 17
  • 5
    Both remove methods are implemented. Why don't you read the doc ? http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html – Alexis C. Nov 12 '13 at 15:32
  • possible duplicate of [Remove Item from ArrayList](http://stackoverflow.com/questions/10714233/remove-item-from-arraylist) –  Nov 12 '13 at 15:33
  • 1
    @ZouZou Because both work and the question is which of them would be the better choice. – blalasaadri Nov 12 '13 at 15:34
  • You'd better use list.remove(b), it's more java-like but it's always the same : if you can do both then it means that both may be useful, depending on the use case. In other words it's up to you man :) – Julien Nov 12 '13 at 15:34
  • @blalasaadri the answer is: test both and decide the better for your needs. In fact, a better option would be using `Iterator#remove` or maybe using a different collection like `Set`. – Luiggi Mendoza Nov 12 '13 at 15:35
  • @LuiggiMendoza Yes, in many cases using an Iterator would be the best choice. Also, it's not me asking the question; I'm just explaining why the question makes sense. – blalasaadri Nov 12 '13 at 15:38
  • 1
    @blalasaadri IMO the question doesn't make any sense if you don't do some research first, and the research is: reading the docs, do some testing... which the question doesn't show at all. – Luiggi Mendoza Nov 12 '13 at 15:41

2 Answers2

3

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().

Eel Lee
  • 3,513
  • 2
  • 31
  • 49
  • 1
    In order to use `List#remove(Object o)` the class should implement `equals` method, and by implementing it the class also should implement `hashCode` as well... – Luiggi Mendoza Nov 12 '13 at 15:34
  • Not only could it change, if you remove several objects the order will probably change unless you remove them in reverse order. So using `remove(Object)` will be the better choice in most cases. – blalasaadri Nov 12 '13 at 15:36
  • The order of the list could change?! – Charles Forsythe Nov 12 '13 at 15:38
  • 1
    @CharlesForsythe that may happen if you use [`List#add(int index, E e)`](http://docs.oracle.com/javase/7/docs/api/java/util/List.html#add%28int,%20E%29) – Luiggi Mendoza Nov 12 '13 at 15:40
  • @LuiggiMendoza I think I see what you're saying. I would not describe that at the order changing, so the description confused me. – Charles Forsythe Nov 12 '13 at 15:41
  • @CharlesForsythe yes, Luiggi Mendoza gave the example. – Eel Lee Nov 12 '13 at 15:41
0

You can do both, removing with the index or the object him self

BilalDja
  • 1,072
  • 1
  • 9
  • 17