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?

4

2 回答 2

3

您可以同时使用两者,但显然最好删除对象,因为理论上顺序可能会改变。

编辑:正如@Luiggi Mendoza 所提到的 -equals()如果你想正确使用remove(Object o). 如果你这样做了,别忘了也覆盖hashCode().

于 2013-11-12T15:33:32.040 回答
0

您可以同时使用索引或对象自己删除

于 2013-11-12T15:35:23.723 回答