A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张然龙 金牌黑马   /  2014-5-9 11:41  /  1940 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张然龙 于 2014-5-9 18:25 编辑

如注释所描述的 ,为什么取反就调用了Person中的equals方法,不取反就不会调用equals??

代码如下↓

  1. class Person
  2. {
  3.         int age;
  4.         String name;
  5.         Person(String name,int age)
  6.         {
  7.                 this.name=name;
  8.                 this.age=age;
  9.         }
  10.         public String getName()
  11.         {
  12.                 return name;
  13.         }
  14.         public int getAge()
  15.         {
  16.                 return age;
  17.         }
  18.         public boolean equals(Object obj)
  19.         {
  20.                 Person per=(Person)obj;
  21.                 System.out.println("进行了equals比较");//这个地方为什么一次都没读过?????
  22.                 return this.name.equals(per.getName())&&this.age==per.getAge();
  23.         }
  24. }
  25. class Single
  26. {
  27.         ArrayList<Person> al1=new ArrayList<Person>();
  28.         public  ArrayList<Person> method(ArrayList<Person> a)
  29.         {
  30.                 for (Iterator<Person> it = a.iterator(); it.hasNext();)
  31.                 {
  32.                         Person per =(Person)it.next();
  33.                         if(al1.contains(per))//这个地方不用!取反,按理来说也会读到Person类中的覆盖方法equals的啊?
  34.                         {
  35.                                 al1.add(per);
  36.                         }
  37.                 }
  38.                 return al1;
  39.         }
  40. }
  41. class Demo4
  42. {
  43.         public static void main(String[] args)
  44.         {
  45.                 ArrayList<Person> al=new ArrayList<Person>();
  46.                 al.add(new Person("1",10));
  47.                 al.add(new Person("1",10));
  48.                 al.add(new Person("1",10));
  49.                 al.add(new Person("1",10));
  50.                 al.add(new Person("1",10));
  51.                 al=new Single().method(al);
  52.                 System.out.println(al);
  53.         }
  54. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 神马都是浮云

查看全部评分

9 个回复

倒序浏览
都没大神回答我.......
回复 使用道具 举报
这应该是涉及到jvm的是如何实现contains的。据我测试数组是空的时候就不会调用equals了。
if (true)
                        {
                                //al1.add(per);//此句话加上或不加上就能看出效果
                                al1.contains(per);

                        }
回复 使用道具 举报
本帖最后由 raikecody 于 2014-5-9 18:13 编辑

不用"!",一样能读到equals没错的,我编译了好几回,结果都是一样的
以下是我找到的contains方法的源代码,里面调用了equals,而LZ又复写了equals,所以“进行了equals比较”这句话只要contains方法一被调用equals肯定也就被调用了,希望我说的没错。

  1.    
  2. public boolean contains(Object o) {
  3.         Iterator<E> it = iterator();
  4.         if (o==null) {
  5.                 while (it.hasNext())
  6.                         if (it.next()==null)
  7.                                 return true;
  8.         } else {
  9.                 while (it.hasNext())
  10.                         if (o.equals(it.next()))
  11.                                 return true;
  12.         }
  13.         return false;
复制代码



评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 赞一个!

查看全部评分

回复 使用道具 举报
将add注释掉就不会比较equals,难道contains还要调用add??  太假了吧??
回复 使用道具 举报
raikecody 发表于 2014-5-9 18:03
不用"!",一样能读到equals没错的,我编译了好几回,结果都是一样的
以下是我找到的contains方法的源代码, ...

我知道问题出哪里了。。。。。

我的al1值是null的  他就不进行equals比较了。。。。。。:Q:Q:Q:Q:Q:Q
回复 使用道具 举报
张然龙 发表于 2014-5-9 18:08
将add注释掉就不会比较equals,难道contains还要调用add??  太假了吧??

那是因为你最下面那行al=new Single().method(al);
才真正用到了用到了Single类中的method方法,

而你之前五句重复的“al.add(new Person("1",10));”
向ArrayList中传入了数据,从而在你调用method方法时,迭代器工作,
进而打印出来“进行equals比较”的这句话。
回复 使用道具 举报
归根结底就是我的al1是null  不调用equals,和equals半毛钱关系都没有。。。。:L
回复 使用道具 举报
顶个啊啊啊啊啊啊啊啊啊啊啊啊啊
回复 使用道具 举报
用了泛型了迭代的时候怎么还强制类型转换去了?嘿嘿
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马