[Java]indexOf是否使用equals?
[Java]Does indexOf use equals?
我想知道 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 return 错误但它应该 return 正确。 (我检查了我要找的元素是否存在)
我完全错了吗?
equals
不应该 return 当比较的对象不是同一类型时为真(在你的情况下 CustomObject
的 equals
应该总是 return 当 o
不是 CustomObject
).
的实例时为 false
indexOf
的实现恰好使用 String
的 equals
而不是 CustomObject
的 equals
当你传递一个 String
给它,当你传递给它一个不是 String
.
的对象时,String
的 equals
return 是 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);
}
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 ? get(i)==null : 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);
我想知道 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 return 错误但它应该 return 正确。 (我检查了我要找的元素是否存在)
我完全错了吗?
equals
不应该 return 当比较的对象不是同一类型时为真(在你的情况下 CustomObject
的 equals
应该总是 return 当 o
不是 CustomObject
).
indexOf
的实现恰好使用 String
的 equals
而不是 CustomObject
的 equals
当你传递一个 String
给它,当你传递给它一个不是 String
.
String
的 equals
return 是 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);
}
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 ? get(i)==null : 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);