当你在数组列表上调用remove(object o)时,如何比较对象? [英] When you call remove(object o) on an arraylist, how does it compare objects?

查看:251
本文介绍了当你在数组列表上调用remove(object o)时,如何比较对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在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 and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

这篇关于当你在数组列表上调用remove(object o)时,如何比较对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆