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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 想进黑马培训 中级黑马   /  2013-8-13 08:48  /  1610 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


下面这段代码 报了一个异常 我该怎么解决呢?xception in thread "main" java.lang.ClassCastException: com.itheima.Student cannot be cast to java.lang.Comparable

package com.itheima;
import java.util.Iterator;
import java.util.TreeSet;
class Student {
   static String name;
   static int age;
   static int score;
Student(String name, int age, int score) {
  Student.name = name;
  Student.age = age;
  Student.score = score;
}
}
public class Test10 {

public static void main(String[] args) {
  // TODO Auto-generated method stub
  Student stu1 = new Student("wangda", 21, 98);
  Student stu2 = new Student("liuer", 25, 95);
  Student stu3 = new Student("zhangsa", 29, 89);
  Student stu4 = new Student("lisi", 25, 92);
  Student stu5 = new Student("zhaowu", 43, 93);
  TreeSet<Student> st = new TreeSet<Student>();
  st.add(stu1);
  st.add(stu2);
  st.add(stu3);
  st.add(stu4);
  st.add(stu5);
  Iterator<Student> it = st.iterator();
  while (it.hasNext()) {
   System.out.println(it.hasNext());
  }
}
}

评分

参与人数 1技术分 +1 收起 理由
神之梦 + 1

查看全部评分

5 个回复

倒序浏览
本帖最后由 狐灵 于 2013-8-13 09:12 编辑

你把 Student类实现Comparable<E>这个接口,并覆写public int compareTo(E e)这个方法,这样就不会报类型转换异常了,然后你再自己跑跑,看看能不能得出你想要的结果。
代码如下
  1. package com.itheima;

  2. import java.util.Iterator;
  3. import java.util.TreeSet;

  4. class Student implements Comparable<Student> {
  5.         static String name;
  6.         static int age;
  7.         static int score;
  8.         
  9.         Student(String name, int age, int score) {
  10.                 Student.name = name;
  11.                 Student.age = age;
  12.                 Student.score = score;
  13.         }

  14.         @Override
  15.         public int compareTo(Student stu) {
  16.                 // 这里需要你自己添加具体怎么比较对象
  17.         return 0;
  18.         }
  19. }

  20. public class Test10 {
  21.         public static void main(String[] args) {
  22.                 Student stu1 = new Student("wangda", 21, 98);
  23.                 Student stu2 = new Student("liuer", 25, 95);
  24.                 Student stu3 = new Student("zhangsa", 29, 89);
  25.                 Student stu4 = new Student("lisi", 25, 92);
  26.                 Student stu5 = new Student("zhaowu", 43, 93);
  27.                 TreeSet<Student> st = new TreeSet<Student>();
  28.                 st.add(stu1);
  29.                 st.add(stu2);
  30.                 st.add(stu3);
  31.                 st.add(stu4);
  32.                 st.add(stu5);
  33.                 Iterator<Student> it = st.iterator();
  34.                 while (it.hasNext()) {
  35.                         System.out.println(it.hasNext());
  36.                 }
  37.         }
  38. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
神之梦 + 1

查看全部评分

回复 使用道具 举报
显示的是布尔类型的值 而且还是死循环。。。无限真
回复 使用道具 举报
哦 看错了!不好意思 谢谢哈
回复 使用道具 举报
本帖最后由 狐灵 于 2013-8-13 12:28 编辑

输出true是因为
  • while (it.hasNext()) {
  •                         System.out.println(it.hasNext());
  •                 }
这个地方直接把it.hasNext())输出了。
我贴出的代码是不完整的。
回复 使用道具 举报
Sudent类要实现Compareable或者Compareator接口。然后去实现各自接口中的方法

Comparable和 Comparator的区别?
一个类实现了Camparable 接口则表明这个类的对象之间是可以相互比较的,这个类对象组成的集合就可以直接使用sort 方法排序。Comparator 可以看成一种算法的实现,将算法和数据分离,Comparator 也可以在下面两种环境下使用:
1、类的没有考虑到比较问题而没有实现Comparable,可以通过Comparator 来实现排序而不必改变对象本身。
2、可以使用多种排序标准,比如升序、降序等。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马