When you call remove(object o) on an arraylist, how does it compare objects?(当您在 arraylist 上调用 remove(object o) 时,它如何比较对象?)
问题描述
当您在 java 中的 arraylist 上调用 remove(object o) 时,它如何比较对象以找到要删除的正确对象?它使用指针吗?还是使用 Comparable 接口比较对象?
When you call remove(object o) on an arraylist in java, how does it compare the objects to find the correct one to remove? does it use the pointer? or does it compare the objects using the interface Comparable?
推荐答案
ArrayList
remove()
依赖于Equal
的对象实现方法.如果没有实现,则该对象被 Object
的 Equals
实现移除,这确实是指针比较.
ArrayList
remove()
relies on the objects implementation of the Equal
method. If no implementation has been done then the object is removed by Object
's implementation of Equals
which indeed is the pointer comparison.
来自 ArrayList
的文档 -
更正式地说,删除具有最低索引 i 的元素,使得 (o==null ? get(i)==null : o.equals(get(i)))
(如果这样一个元素存在)
More formally, removes the element with the lowest index i such that
(o==null ? get(i)==null : o.equals(get(i)))
(if such an element exists)
Object equal
方法文档 -
Object equal
method documentation -
Object类的equals方法实现了对象上最有区别的可能等价关系;也就是说,对于任何非空引用值 x
和 y
,当且仅当 x
和 y
引用同一个对象(x == y
的值为 true
).
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values
x
andy
, this method returns true if and only ifx
andy
refer to the same object (x == y
has the valuetrue
).
这篇关于当您在 arraylist 上调用 remove(object o) 时,它如何比较对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当您在 arraylist 上调用 remove(object o) 时,它如何比较对象?


- 转换 ldap 日期 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 获取数字的最后一位 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01