List在执行remove方法不能删除指定的对象
2017-05-22 22:07
330 查看
List在执行remove方法不能删除指定的对象
我们根据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在删除对象时,先判断这个对象是否在自己的队列中?而这种判断指的是是否equals;因此,List在删除对象时,如果使用删除对象方法,应该最好重写equals方法。或者采用删除下标的方法。
删除下标时一定要确保下标的类型是int类型,若是Integer类型,List会默认匹配remove(Object o)方法,而不是remove(int index)方法。
相关文章推荐
- 关于List,ArrayList中remove(object obj)方法不能删除对象问题
- 泛型List<T>中方法Remove不能删除对象的解决
- 为什么Iterator的remove方法可保证从源集合中安全地删除对象,而在迭代期间不能直接删除集合内元素
- C#中 List类型Remove()方法不能移除对象问题
- Arrays.asList方法返回的List不能执行add、remove操作
- 将指定SQL的执行计划从共享池删除的方法
- java 中执行list 的remove 方法
- list remove不能删除后一项目的办…
- jquery使用remove()方法删除指定class子元素
- 链表-Remove Nth Node From End of List(删除指定的节点)
- 为什么Java里的Arrays.asList不能用add和remove方法?
- 【Android】ArrayList通过remove方法删除元素对象源码分析
- Java中list的remove方法移除的对象与下标重合了呢?
- Iterator的remove方法可保证从源集合中安全地删除对象
- Remove Linked List Elements 删除链表中指定元素
- list中删除元素的方法:pop(x)和remove(x)
- C# List<Apple>删除指定的对象
- Java中List对象调用remove方法报错:UnsupportedOperationException
- 为什么Java里的Arrays.asList不能用add和remove方法?
- 遍历list集合删除指定元素方法