黑马程序员技术交流社区

标题: 郁闷死我,不知道错在那、、、、 [打印本页]

作者: 黄泉    时间: 2014-6-3 00:20
标题: 郁闷死我,不知道错在那、、、、
  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都没事,一用对象就不行。。。

作者: ゞ导火索゛    时间: 2014-6-3 00:34
本帖最后由 ゞ导火索゛ 于 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. }
复制代码




作者: Autumn    时间: 2014-6-3 08:16
你这么长的的代码,看着就烦。
作者: 黄泉    时间: 2014-6-3 12:44
ゞ导火索゛ 发表于 2014-6-3 00:34
张三李四是字符串,不能作为参数实例化Integer,看你自己代码23句,还有14句的输出语句

感谢!!!!!!!!
作者: 墓____夜    时间: 2014-6-3 13:27
new mycomparator 后边加个泛型,同时你比较器写的有问题,比较器年龄不需要加integer




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2