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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈国柱 中级黑马   /  2013-8-12 20:05  /  1234 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

1.按照毕老师的教程里面说,只有一个ts.add(new Student("lisi02",22)); 时,下面的代码应该编译和运行都成功的,但是不知道为什么我的这个就是运行没有通过,运行后显示ClassCastException.
  1. import java.util.*;

  2. class TreeSetTesting
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 TreeSet ts = new TreeSet();

  7.                 ts.add(new Student("lisi02",22));

  8.                 Iterator it = ts.iterator();
  9.                 while (it.hasNext())
  10.                 {
  11.                         Student stu = (Student)it.next();
  12.                         System.out.println(stu.getName()+"......"+stu.getAge());
  13.                 }
  14.         }
  15. }
  16. class Student
  17. {
  18.         private String name;
  19.         private int age;

  20.         Student(String name,int age)
  21.         {
  22.                 this.name = name;
  23.                 this.age = age;
  24.         }
  25.         public String getName()
  26.         {
  27.                 return name;
  28.         }
  29.         public int getAge()
  30.         {
  31.                 return age;
  32.         }
  33. }
复制代码
2.之后继续完善这个代码,发现我的代码运行后比视频所说的多比较了一次,第一个对象自己和自己比较了,可能就是这样才会导致上面的代码运行不成功,但是为什么是这样的呢?
  1. import java.util.*;

  2. class TreeSetTesting1
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 TreeSet ts = new TreeSet();

  7.                 ts.add(new Student("lisi02",22));
  8.                 ts.add(new Student("lisi02",23));

  9.                 Iterator it = ts.iterator();
  10.                 while (it.hasNext())
  11.                 {
  12.                         Student stu = (Student)it.next();
  13.                         System.out.println(stu.getName()+"......"+stu.getAge());
  14.                 }
  15.         }
  16. }
  17. class Student implements Comparable
  18. {
  19.         private String name;
  20.         private int age;

  21.         Student(String name,int age)
  22.         {
  23.                 this.name = name;
  24.                 this.age = age;
  25.         }
  26.         public int compareTo(Object obj)
  27.         {
  28.                 if(!(obj instanceof Student))
  29.                         throw new RuntimeException("not a student");
  30.                 Student s = (Student)obj;
  31.                 System.out.println(this.name+"::"+this.age+"...CompareTo..."+s.name+"::"+s.age);

  32.                 if(this.age>s.age)
  33.                         return 1;
  34.                 if(this.age==s.age)
  35.                         return this.name.compareTo(s.name);
  36.                 return -1;
  37.         }
  38.         public String getName()
  39.         {
  40.                 return name;
  41.         }
  42.         public int getAge()
  43.         {
  44.                 return age;
  45.         }
  46. }
复制代码

评分

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

查看全部评分

3 个回复

倒序浏览
当添加两个学生信息时同时比较时时不可取的。由于是学生的多对象,需要存储起来。学生自身不具有比较性。当只要同时可以创建对个对象时那么,这些对象需要被用的时候,没有复写compareTo()方法,并指定泛型。这些动作时,执行时系统会报java.lang.ClassCastException(也就是说:当试图将对象强制转换为不是实例的子类时,将会抛出该异常)。那么为了让就有比较性需要对其进行复写compareTo()方法,并指定泛型。所以在复写compareTo()方法时是不会 出错的。希望有助于理解!
  1. class TreeSetTesting1
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 TreeSet ts = new TreeSet();
  6.                 ts.add(new Student("lisi02",22));
  7.                 ts.add(new Student("lisi02",23));
  8.                 //获取一个其迭代器
  9.                 Iterator it = ts.iterator();
  10.                 while (it.hasNext())
  11.                 {
  12.                         Student stu = (Student)it.next();
  13.                         System.out.println(stu.getName()+"......"+stu.getAge());
  14.                 }
  15.         }
  16. }
  17. class Student implements Comparable
  18. {
  19.         private String name;
  20.         private int age;

  21.         Student(String name,int age)
  22.         {
  23.                 this.name = name;
  24.                 this.age = age;
  25.         }
  26.         public int compareTo(Object obj)
  27.         {
  28.                 if(!(obj instanceof Student))
  29.                         throw new RuntimeException("not a student");
  30.                 Student s = (Student)obj;
  31.                 System.out.println(this.name+"::"+this.age+"...CompareTo..."+s.name+"::"+s.age);

  32.                 if(this.age>s.age)
  33.                         return 1;
  34.                 if(this.age==s.age)
  35.                         return this.name.compareTo(s.name);
  36.                 return -1;
  37.         }
  38.         public String getName()
  39.         {
  40.                 return name;
  41.         }
  42.         public int getAge()
  43.         {
  44.                 return age;
  45.         }
  46. }
复制代码
望互助学习!
回复 使用道具 举报
第一个只有一个ts.add,不应该运行不成功的吧?
而第二个运行后不知道为什么多了一次第一个数据的自身比较了

第二个代码运行后结果.jpg (16.14 KB, 下载次数: 1)

第二个

第二个

第一个代码运行后结果.jpg (33.4 KB, 下载次数: 1)

第一个

第一个
回复 使用道具 举报
回答楼主的第一个问题:为什么TreeSet在自定义存储对象时,只存入一个对象就报错?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马