gpt4 book ai didi

java - 当您在 arraylist 上调用 remove(object o) 时,它如何比较对象?

转载 作者:IT老高 更新时间:2023-10-28 21:12:55 25 4
gpt4 key购买 nike

当您在 java 中的 arraylist 上调用 remove(object o) 时,它如何比较对象以找到要删除的正确对象?它使用指针吗?还是使用 Comparable 接口(interface)比较对象?

最佳答案

ArrayList remove() 依赖于 Equal 方法的对象实现。如果没有实现,则对象被 ObjectEquals 实现移除,这确实是指针比较。

来自 ArrayList 的文档 -

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 方法文档 -

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).

关于java - 当您在 arraylist 上调用 remove(object o) 时,它如何比较对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2894618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com