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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 我是庄子 中级黑马   /  2015-10-23 20:38  /  3341 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

5黑马币
在毕向东的15天02的TreeSet自定义对象那节视频中,按照视频中打出代码,就是剩下最后一个ts.add(new Student("lisi02",22));
还是结果是Exception in thread "main" java.lang.ClassCastExceptio
      to java.lang.Comparable
        at java.util.TreeMap.compare(Unknown Source)
        at java.util.TreeMap.put(Unknown Source)
        at java.util.TreeSet.add(Unknown Source)
        at TreeSetDemo1.main(TreeSetDemo1.java:25)


最佳答案

查看完整内容

类型转换异常,Student类不能向上转型成变量ts的那个类型

5 个回复

倒序浏览
类型转换异常,Student类不能向上转型成变量ts的那个类型
回复 使用道具 举报
类型转换异常,你的Student类不可比较。你的Student类没有实现compareTo()方法或者没有也没传比较器
回复 使用道具 举报
package com.itheima;
import java.util.TreeSet;

/**
* 声明类Student,包含3个成员变量:name、age、score,创建5个对象装入TreeSet,按照成绩排序输出结果(考虑成绩相同的问题)
* @author Administrator
*
*/

public class Test10 {
        public static void main(String[] args){
                //创建Student对象
                Student stu = new Test10().new Student("aaa", 18, 85);
                Student stu1 = new Test10().new Student("bbb", 19, 97);
                Student stu2 = new Test10().new Student("ccc", 13, 97);
                Student stu3 = new Test10().new Student("ddd", 20, 87);
                Student stu4 = new Test10().new Student("eee", 18, 57);
                //创建TreeSet对象
                TreeSet<Student> treeSet = new TreeSet<Student>();
                treeSet.add(stu);
                treeSet.add(stu1);
                treeSet.add(stu2);
                treeSet.add(stu3);
                treeSet.add(stu4);
                //打印treeset集合
                System.out.println(treeSet);
        }
        /**
         *
         * @author Administrator
         *decription(学生类),实现Comparable接口
         */
        class Student implements Comparable{
                /**field  name 姓名*/
                private String name;
               
                /**field  age 年龄*/
                private int age;
               
                /**field  score 分数*/
                private float score;
               
                /**构造方法*/
                public Student(String name,int age,float score){
                        this.name = name;
                        this.age = age;
                        this.score = score;
                }
               
                public String getName(){
                        return this.name;
                }
                public void setName(String name){
                        this.name = name;
                }
                public int getAge(){
                        return this.age;
                }
                public void setAge(int age){
                        this.age = age;
                }
                public float getScore(){
                        return this.score;
                }
                public void setScore(float score){
                        this.score = score;
                }
                 //按顺序输出时的比较方法
                @Override
                public int compareTo(Object o) {
                        Student student  = (Student)o;
                        //先按照分数比较,分数相同按照名字
                        if(this.score > student.score){
                                return -1;
                        }else if(this.name.compareTo(student.name)>0){
                                return 1;
                        }else if(this.name.compareTo(student.name)<0){
                                return -1;
                        }
                        return 0;
                }
               
                @Override
                public String toString(){
                        return "[姓名:"+name+",年龄:"+age+",分数:"+score+"]";
                }

               
        }
}
回复 使用道具 举报
TreeSet 存储对象有两个方案;
一:调用构造方法:new TreeSet(Comparator c)
二:被添加对象所属的类实现Comparable接口
因为TreeSet  是要对被添加对象进行排序的。
所以要么TreeSet本身有一个比较器,要么被添加对象本身具有可比性(就是比较大小的能力)
回复 使用道具 举报
来晚了,已经被你们解决了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马