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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黄泉 中级黑马   /  2014-6-3 00:20  /  1158 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.*;

  2. class Demo
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());
  7.                 ts.add(new Student("张三",20));
  8.                 ts.add(new Student("李四",25));
  9.                 ts.add(new Student("张三",22));

  10.                 for (Student s : ts)
  11.                 {
  12.                         println.sop(s.toString());
  13.                 }
  14.         }
  15. }

  16. class MyComparator implements Comparator<Student>
  17. {
  18.         public int compare(Student s1,Student s2)
  19.         {
  20.                 int num = new Integer(s1.getName()).compareTo(new Integer(s2.getName()));
  21.        
  22.                 if(num == 0)
  23.                         return  new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));

  24.                 return num;
  25.         }
  26. }

  27. class Student implements Comparable<Student>
  28. {
  29.         private String name;
  30.         private int age;
  31.         Student(String name,int age)
  32.         {
  33.                 this.name = name;
  34.                 this.age = age;
  35.         }
  36.        

  37.         public String getName()
  38.         {
  39.                 return name;
  40.         }
  41.         public int getAge()
  42.         {
  43.                 return age;
  44.         }

  45.         public String toString()
  46.         {
  47.                 return "姓名:"+name+",年龄"+age;
  48.         }

  49.         public boolean equals(Object obj)
  50.         {
  51.                 if(!(obj instanceof Student))
  52.                         throw new RuntimeException("数据类型错误");

  53.                 Student s = (Student)obj;

  54.                 return this.name.equals(s.name) && this.age == s.age;
  55.         }

  56.         public int hashCode()
  57.         {
  58.                 return name.hashCode()+age*13;
  59.         }

  60.         public int compareTo(Student s)
  61.         {
  62.                 int num = new Integer(this.name).compareTo(new Integer(s.name));
  63.        
  64.                 if(num == 0)
  65.                         return  new Integer(this.age).compareTo(new Integer(s.age));

  66.                 return num;
  67.         }
  68. }
复制代码


搞了n次,死活都不知道错在哪里。
用String都没事,一用对象就不行。。。

评分

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

查看全部评分

4 个回复

倒序浏览
本帖最后由 ゞ导火索゛ 于 2014-6-3 00:45 编辑

张三李四是字符串,不能作为参数实例化Integer,看你自己代码23句,还有14句的输出语句


  1. import java.util.*;

  2. class Demo
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());
  7.                 ts.add(new Student("张三",20));
  8.                 ts.add(new Student("李四",25));
  9.                 ts.add(new Student("张三",22));

  10.                 for (Student s : ts)
  11.                 {
  12.                         System.out.println(s.toString());
  13.                 }
  14.         }
  15. }

  16. class MyComparator implements Comparator<Student>
  17. {
  18.         public int compare(Student s1,Student s2)
  19.         {        
  20.                         //这一句改成这样
  21.                 int num = s1.getName().compareTo(s2.getName());

  22.                 if(num == 0)
  23.                         return  new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));

  24.                 return num;
  25.         }
  26. }

  27. class Student implements Comparable<Student>
  28. {
  29.         private String name;
  30.         private int age;
  31.         Student(String name,int age)
  32.         {
  33.                 this.name = name;
  34.                 this.age = age;
  35.         }


  36.         public String getName()
  37.         {
  38.                 return name;
  39.         }
  40.         public int getAge()
  41.         {
  42.                 return age;
  43.         }

  44.         public String toString()
  45.         {
  46.                 return "姓名:"+name+",年龄"+age;
  47.         }

  48.         public boolean equals(Object obj)
  49.         {
  50.                 if(!(obj instanceof Student))
  51.                         throw new RuntimeException("数据类型错误");

  52.                 Student s = (Student)obj;

  53.                 return this.name.equals(s.name) && this.age == s.age;
  54.         }

  55.         public int hashCode()
  56.         {
  57.                 return name.hashCode()+age*13;
  58.         }

  59.         public int compareTo(Student s)
  60.         {
  61.                 int num = new Integer(this.name).compareTo(new Integer(s.name));

  62.                 if(num == 0)
  63.                         return  new Integer(this.age).compareTo(new Integer(s.age));

  64.                 return num;
  65.         }
  66. }
复制代码



评分

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

查看全部评分

回复 使用道具 举报
你这么长的的代码,看着就烦。
回复 使用道具 举报
ゞ导火索゛ 发表于 2014-6-3 00:34
张三李四是字符串,不能作为参数实例化Integer,看你自己代码23句,还有14句的输出语句

感谢!!!!!!!!
回复 使用道具 举报
墓____夜 来自手机 中级黑马 2014-6-3 13:27:14
报纸
new mycomparator 后边加个泛型,同时你比较器写的有问题,比较器年龄不需要加integer
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马