当你在数组列表上调用remove(object o)时,如何比较对象? [英] When you call remove(object o) on an arraylist, how does it compare objects?
问题描述
当在java中的数组列表上调用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类的equals方法实现了对象上最具辨别性的可能的等价关系;也就是说,对于任何非空引用值
x
和y
,此方法仅当< c $ c> 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
).
这篇关于当你在数组列表上调用remove(object o)时,如何比较对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!