通过看List remove方法我们就能得到答案了
public boolean remove(Object o){ if(o ==null) { for(intindex=0;index< size;index++) if(elementData[index] ==null) { fastRemove(index);returntrue; } }else{ for(intindex=0;index< size;index++) if(o.equals(elementData[index])) { fastRemove(index);returntrue; } } return false; }
List进入remove方法,先判断这个对象是否在自己的队列中?而这种判断指的是是否equals;
因此,List在删除对象时,如果使用删除对象方法,应该最好重写equals方法。或者采用删除下标的方法。
删除下标时一定要确保下标的类型是int类型,若是Integer类型,List会默认匹配remove(Object o)方法,而不是remove(int index)方法。
做项目突然想到的一个问题做个笔记分享一下