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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© doyxy 中级黑马   /  2014-1-13 17:04  /  961 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

TreeSet中,毕老师的视频里只有一个元素时可以正常添加,为什么我只添加一个元素也报错呢?
Exception in thread "main" java.lang.ClassCastException: Student cannot be cast
to java.lang.Comparable
        at java.util.TreeMap.compare(TreeMap.java:1188)
        at java.util.TreeMap.put(TreeMap.java:531)
        at java.util.TreeSet.add(TreeSet.java:255)
        at TreeSetDemo.main(TreeSetDemo.java:19)

  1. /*
  2. TreeSet: 可以对Set集合中的元素进行排序.

  3. 需求:往TreeSet集合中存储学生,按年龄排序.
  4. */

  5. import java.util.*;

  6. class  TreeSetDemo
  7. {
  8.         public static void main(String[] args)
  9.         {
  10.                 TreeSet ts = new TreeSet();
  11. //                ts.add("adc");
  12. //                ts.add("aec");
  13. //                ts.add("Bdbc");//大写字母数值较小,排在小写前面.
  14. //                ts.add("abc");

  15.                 ts.add(new Student("lisi05",26));
  16. //                ts.add(new Student("lisi02",21));
  17. //                ts.add(new Student("lisi04",25));
  18. //                ts.add(new Student("lisi03",22));

  19.                 Iterator it = ts.iterator();
  20.                 while (it.hasNext())
  21.                 {
  22.                         Student stu = (Student)it.next();
  23.                         sop(stu.getName()+"..."+stu.getAge());
  24.                 }
  25.         }
  26.         public static void sop(Object obj)
  27.         {
  28.                 System.out.println(obj);
  29.         }

  30. }
  31. class Student
  32. {
  33.         private String name;
  34.         private int age;
  35.         Student(String name, int age)
  36.         {
  37.                 this.name = name;
  38.                 this.age = age;
  39.         }
  40.         public String getName()
  41.         {
  42.                 return name;
  43.         }
  44.         public int getAge()
  45.         {
  46.                 return age;
  47.         }
  48. }
复制代码



评分

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

查看全部评分

6 个回复

倒序浏览
我执行成功了、、、、、!!!!
回复 使用道具 举报
http://blog.csdn.net/you_off3/article/details/7465919
回复 使用道具 举报
我关闭了编辑器和命令行重新来也还是报错,怎么回事啊
回复 使用道具 举报
TreeMap中的对象不一定都具有比较性,所以要implements Comparable。可以这么写:

//该接口强制让学生具备比较性。
class Student implements Comparable
{
        private String name;
        private int age;

        Student(String name,int age)
        {
                this.name = name;
                this.age = age;
        }

        public int compareTo(Object obj)
        {

                //return 0;
               
                if(!(obj instanceof Student))
                        throw new RuntimeException("不是学生对象");
                Student s = (Student)obj;

                System.out.println(this.name+"....compareto....."+s.name);
                if(this.age>s.age)
                        return 1;
                if(this.age==s.age)
                {
                        return this.name.compareTo(s.name);
                }
                return -1;
                /**/
        }

        public String getName()
        {
                return name;

        }
        public int getAge()
        {
                return age;
        }
}

评分

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

查看全部评分

回复 使用道具 举报
快乐的黑马 发表于 2014-1-13 17:34
TreeMap中的对象不一定都具有比较性,所以要implements Comparable。可以这么写:

//该接口强制让学生具备 ...

实现比较性后是正常的,但是看视频里,没有实现之前,只添加一个元素时可以正常运行,而我的会报错
回复 使用道具 举报
一个元素按理来说不会报错啊。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马