When you call remove(object o) on an arraylist, how does it compare objects?

前端 未结 4 1878
借酒劲吻你
借酒劲吻你 2020-12-06 09:13

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 ob

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-06 09:48

    It uses equals()

    from the docs:

    Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. 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). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

提交回复
热议问题