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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 中关村阿旺 于 2013-10-19 20:44 编辑
  1. package demo3;
  2. import java.util.*;

  3. class Student implements Comparable<Student>
  4. {
  5. public int compareTo(Student s)
  6. {
  7. int num=name.compareTo(s.name);
  8. if(num==0)
  9. return new Integer(id).compareTo(new Integer(s.id));
  10. return num;
  11. }

  12. private String id;
  13. private String name;

  14. public String getId()
  15. {
  16. return id;
  17. }

  18. public String getName()
  19. {
  20. return name;
  21. }

  22. Student(String id,String name)
  23. {
  24. this.id=id;
  25. this.name=name;
  26. }

  27. public String toString()
  28. {
  29. return id+"→→→"+name;
  30. }
  31. }

  32. class NewComparator implements Comparator<Student>
  33. {
  34. public int compare(Student s1,Student s2)
  35. {
  36. int num=new Integer(s1.getId()).compareTo(new Integer(s2.getId()));
  37. if(num==0)
  38. return s1.getName().compareTo(s2.getName());
  39. return num;
  40. }
  41. }

  42. class CollectionsDemo3
  43. {
  44. public static void print(List<Student> list)
  45. {
  46. for (Iterator<Student> it=list.iterator();it.hasNext() ; )
  47. {
  48. Student s=it.next();
  49. System.out.println(s);
  50. }
  51. System.out.println("========================================");
  52. }

  53. public static void main(String[] args)
  54. {
  55. List<Student> list=new ArrayList<Student>();
  56. list.add(new Student("02","lisi"));
  57. list.add(new Student("03","wangwu"));
  58. list.add(new Student("01","zhangsan"));
  59. list.add(new Student("03","wangwu"));
  60. list.add(new Student("04","zhaoliu"));
  61. list.add(new Student("05","zhangsan"));
  62. System.out.println("原集合是:");
  63. print(list);

  64. //使用另一个值替换列表中出现的所有某一指定值。
  65. Student s1=new Student("03","wangwu");
  66. Student s2=new Student("06","tianqi");
  67. boolean replace=Collections.replaceAll(list,s1,s2);
  68. System.out.println("是否成功替换:"+replace);
  69. System.out.println("现集合是:");
  70. print(list);

  71. System.out.println("*************************************");

  72. List<String> list2=new ArrayList<String>();
  73. list2.add("abcd");
  74. list2.add("ahter");
  75. list2.add("sfqag");
  76. list2.add("adgw");
  77. list2.add("abcd");
  78. list2.add("gfjf");
  79. System.out.println("原集合是:"+list2);
  80. Collections.replaceAll(list2,"abcd","haha");
  81. System.out.println("替换后的集合是:"+list2);
  82. }
  83. }
复制代码
为什么String类型的数据可以替换成功,而我写的Student类就是不行呢?!!

有问题.png (69.64 KB, 下载次数: 43)

为什么就是替换不了呢?

为什么就是替换不了呢?

评分

参与人数 1技术分 +1 收起 理由
特殊服务 + 1

查看全部评分

4 个回复

倒序浏览
你的Student类没有覆盖 equals方法啊,其实集合中并没有你后边新建的s1。添加一个 equals方法就行了,最好同时添加 hashCode方法。
回复 使用道具 举报
List集合判断元素是否相同,或者删除指定的元素,依据的是元素的equals方法。如果向List容器中添加类对象,判断集合内的元素是否相同,即判断传入容器的对象用equals方法比较是否相同。如果用默认的equals方法,则判断对象创建时的哈希值。如果要按照指定的判断方式,需要重写类内的equals方法。所以你只需要复写一个equals方法就行啦。
我给你添加一个:
这样就能得出正确的结果啦。

  1. import java.util.*;

  2. class Student implements Comparable<Student>
  3. {
  4.         public int compareTo(Student s)
  5.         {
  6.                 int num=name.compareTo(s.name);
  7.                 if(num==0)
  8.                 return new Integer(id).compareTo(new Integer(s.id));
  9.                 return num;
  10.         }

  11.         private String id;
  12.         private String name;
  13.        
  14.         public String getId()
  15.         {
  16.                 return id;
  17.         }
  18.        
  19.         public String getName()
  20.         {
  21.                 return name;
  22.         }
  23.        
  24.         Student(String id,String name)
  25.         {
  26.                 this.id=id;
  27.                 this.name=name;
  28.         }
  29.        
  30.         public String toString()
  31.         {
  32.                 return id+"→→→"+name;
  33.         }
  34.        
  35.         public boolean equals(Object obj) {
  36.                 if (!(obj instanceof Student))
  37.                         return false;
  38.                 Student p = (Student) obj;
  39.                 if ((this.id == p.id) && (this.name.equals(p.name)))
  40.                         return true;
  41.                 return false;
  42.         }
  43. }

  44. class NewComparator implements Comparator<Student>
  45. {
  46.         public int compare(Student s1,Student s2)
  47.         {
  48.                 int num=new Integer(s1.getId()).compareTo(new Integer(s2.getId()));
  49.                 if(num==0)
  50.                 return s1.getName().compareTo(s2.getName());
  51.                 return num;
  52.         }
  53. }

  54. class CollectionsDemo3
  55. {
  56.         public static void print(List<Student> list)
  57.         {
  58.                 for (Iterator<Student> it=list.iterator();it.hasNext() ; )
  59.                 {
  60.                         Student s=it.next();
  61.                         System.out.println(s);
  62.                 }
  63.                 System.out.println("========================================");
  64.         }
  65.        
  66.         public static void main(String[] args)
  67.         {
  68.                 List<Student> list=new ArrayList<Student>();
  69.                 list.add(new Student("02","lisi"));
  70.                 list.add(new Student("03","wangwu"));
  71.                 list.add(new Student("01","zhangsan"));
  72.                 list.add(new Student("03","wangwu"));
  73.                 list.add(new Student("04","zhaoliu"));
  74.                 list.add(new Student("05","zhangsan"));
  75.                 System.out.println("原集合是:");
  76.                 print(list);
  77.                
  78.                 //使用另一个值替换列表中出现的所有某一指定值。
  79.                 Student s1=new Student("03","wangwu");
  80.                 Student s2=new Student("06","tianqi");
  81.                 boolean replace=Collections.replaceAll(list,s1,s2);
  82.                 System.out.println("是否成功替换:"+replace);
  83.                 System.out.println("现集合是:");
  84.                 print(list);
  85.                
  86.                 System.out.println("*************************************");
  87.                
  88.                 List<String> list2=new ArrayList<String>();
  89.                 list2.add("abcd");
  90.                 list2.add("ahter");
  91.                 list2.add("sfqag");
  92.                 list2.add("adgw");
  93.                 list2.add("abcd");
  94.                 list2.add("gfjf");
  95.                 System.out.println("原集合是:"+list2);
  96.                 Collections.replaceAll(list2,"abcd","haha");
  97.                 System.out.println("替换后的集合是:"+list2);
  98.         }
  99. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
特殊服务 + 1

查看全部评分

回复 使用道具 举报 1 0
谢谢你的回复。{:soso_e113:}
回复 使用道具 举报 1 0
记住一点:遇到集合,元素为自定义类型,不管用不用的到,重写equals和HashCode()方法就是了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马