【问题标题】:[Java]Does indexOf use equals?[Java] indexOf 是否使用等于?
【发布时间】:2016-05-24 13:37:07
【问题描述】:

我想知道 ArrayList 的方法 indexOf 是如何实现的。事实上,我已经像这样覆盖了 equals 方法:

public class CustomObject {
@Override 
    public boolean equals(Object o) {

        if(o instanceof CityLoader)
            return ((CityLoader)o).getName() == this.name;
        else if (o instanceof String)
            return this.name.equals((String)o);         
        return false;
    }
}

虽然这将避免我也覆盖 indexOf 方法,但似乎我完全错了。 当我尝试时

ArrayList<CustomObject> customObjects = new ArrayList<CustomObject>
... insert customobject into the arraylist ...
customObjects.indexOf(new String("name")) 

indexOf 返回 false 但它应该返回 true。 (我检查了我要查找的元素是否存在)

我完全错了吗?

【问题讨论】:

  • "我想知道一个ArrayList的方法indexOf是如何实现的"你不需要知道它是如何实现的,你需要知道知道合同是什么。有一个工具:The JavaDoc,它说:“返回此列表中指定元素第一次出现的索引,如果此列表不包含该元素,则返回 -1。更正式地说,返回最低的索引 i 使得 (o==null ? get(i)==null : o.equals(get(i))),如果没有这样的索引,则为 -1。"

标签: java overriding equals indexof


【解决方案1】:

equals 不应该在比较对象不属于同一类型时返回 true(在您的情况下,CustomObjectequals 应该在 o 不是 CustomObject 的实例时始终返回 false)。

indexOf 的实现恰好使用Stringequals 而不是您的CustomObjectequals,当您将String 传递给它时,以及Stringequals当你向它传递一个不是 String 的对象时返回 false。

另外,不要使用== 来比较字符串。

您应该将CustomObject 的实例传递给indexOf

customObjects.indexOf(new CustomObject("name")) 

(或CustomObject 的构造函数看起来像什么)

您的 equals 方法应该如下所示:

public boolean equals(Object o) {
    if(!(o instanceof CityLoader))
        return false;
    CityLoader other = (CityLoader)o;
    return other.name.equals(this.name);
}

【讨论】:

  • 你是对的!我应该覆盖字符串等于吗?
  • @user1315621 不,你不能。字符串是最终类。
  • 谢谢。所以这似乎是一种可怕的做法。我会避免它。谢谢!
  • 只是另一个问题。如果我想重写 indexOf 方法,我必须在 CustomObject 类中还是在扩展 ArrayList 的新类中进行?
  • @user1315621 indexOf 是List 接口的一个方法。您必须在您正在使用的 List 实现的子类中覆盖它(即在您的情况下是 ArrayList 的子类)。不过,这似乎不是一个好主意。
【解决方案2】:
customObjects.indexOf(new String("name")) 

这就是你做错了。您正在 CustomObject 对象列表中寻找 String 的索引。

来自 java 文档:

 /**
     * Returns the index of the first occurrence of the specified element
     * in this list, or -1 if this list does not contain the element.
     * More formally, returns the lowest index <tt>i</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
     * or -1 if there is no such index.
     *
     * @param o element to search for
     * @return the index of the first occurrence of the specified element in
     *         this list, or -1 if this list does not contain the element
     * @throws ClassCastException if the type of the specified element
     *         is incompatible with this list
     *         (<a href="Collection.html#optional-restrictions">optional</a>)
     * @throws NullPointerException if the specified element is null and this
     *         list does not permit null elements
     *         (<a href="Collection.html#optional-restrictions">optional</a>)
     */
    int indexOf(Object o);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    相关资源
    最近更新 更多