JAVA基础List的contains()方法遇到的问题

weixin_42647822 2018-10-15 07:26:46
两个逻辑上相等的自定义对象obj1和obj2,用ArrayList对象list加入obj1对象,list对象调用contains方法判断是否包含obj2对象,返回值是false,不应该是true吗,两个对象逻辑相等啊。而我用两个逻辑相等的String对象(new两个)就会得到true为什么??
public class Test {
private String value=null;

public Test(String value) {
this.value = value;
}
public boolean equals(Test o){
if (o==this){
return true;
}
if (o instanceof Test){
Test t=(Test) o;
return value.equals(t.value);
}

return false;
}

public static void main(String[] args){
List list=new ArrayList();
Test test1=new Test("obj");
Test test2=new Test("obj");
list.add(test1);
System.out.println(test1.equals(test2));//true
System.out.println(list.contains(test2));//false
/**
* 换两个逻辑上相等的String类对象就没问题,contains()输出true
*/
}


}
...全文
1520 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_41091056 2019-06-11
  • 打赏
  • 举报
回复
list 的contains方法被调用时,会调用底层object类的equals方法,你虽然在test类中写了一个equals方法,但是不是覆盖object类的equals方法,object类中equals是这样定义的public boolean equals(Object obj),你只需将传入的Test o改成 Object o即可
SmallbenClock 2018-10-21
  • 打赏
  • 举报
回复
类是比较类的地址,因为传入方法的是地址
String是比较String的值,因为传入方法的是值
张先 2018-10-16
  • 打赏
  • 举报
回复
可能对象不同,不能比较吧。
weixin_42647822 2018-10-16
  • 打赏
  • 举报
回复
引用 1 楼 LCL_data 的回复:
test1和test2是两个不同的实例。内存地址是不一样的。


System.out.println(test1.equals(test2));//true
是因为最后执行的是
return value.equals(t.value); 就是string.equals方法。 都是obj当然相等



/**
* Compares this string to the specified object. The result is {@code
* true} if and only if the argument is not {@code null} and is a {@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
你好,你回答的和我问的有联系吗?我问的是为什么逻辑上相等的两个对象,list包含了其中一个对象,为什么调用contains()方法返回false
十八道胡同 2018-10-15
  • 打赏
  • 举报
回复
test1和test2是两个不同的实例。内存地址是不一样的。


System.out.println(test1.equals(test2));//true
是因为最后执行的是
return value.equals(t.value); 就是string.equals方法。 都是obj当然相等



/**
* Compares this string to the specified object. The result is {@code
* true} if and only if the argument is not {@code null} and is a {@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧