java中equals方法的问题

来源:百度知道 编辑:UC知道 时间:2024/05/21 19:02:31
import java.util.*;
public class Test {
public static void main(String []agrs)
{
Collection c=new ArrayList();

c.add("hello");

c.add(new Integer(100));
c.remove("hello");
System.out.println(c.size());
System.out.println(c);
System.out.println(c.remove("hello"));
}
}

输出容器C的值时 没有hello
为什么 System.out.println(c.remove("hello")); 返回值是flase?
此时需要重写equals方吗?在那添加?

对于remove方法 在什么情况下 返回 true和flase?
被搞糊涂达··
System.out.println(c.remove("hello")); 我理解的是
是否删除成功
而c.remove("hello");
删除成功···
所以 应该返回true·····

c.add(new Integer(100));
c.remove("hello");

删除成功返回真,否则返回假
但你请注意我Copy的这两行,在这里面你已经调用remove方法把字符串给删除掉了

下面又调用这个方法,因为该串已经不存在于集合中,当然会返回false

你已经把"hello"删了,再删当然返回false了

你在System.out.println(c.remove("hello"));
之前已经写了一句
c.remove("hello");

你说后面的System.out.println(c.remove("hello"));会不会返回true呢?

你仔细看代码System.out.println(c.remove("hello")); 你已经删除了,所以已经是false了。

remove方法是一个移除方法,在顺利执行该操作的时候返回true,执行失败时候返回false,你的c里面没有hello 所以 remove失败返回false

Collection c=new ArrayList();

c.add("hello");

c.add(new Integer(100));
c.remove("hello");
你之前已经remove过了,
System.out.println(c);
当然就没有了
System.out.println(c.remove("hello"));
同样也返回的FLASE
c:remove后如果collection发生的改变返回true,如果没有返回false

c.remove方法执行了两次