- import java.util.*;
- class Student
- {
- private String name;
- private int age;
- Student(String name,int age)
- {
- this.name=name;
- this.age=age;
- }
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- }
- class test
- {
- public static void main(String[] args)
- {
- TreeSet ts=new TreeSet();
- ts.add(new Student("lisi06",23));
-
- Iterator it=ts.iterator();
- while (it.hasNext())
- {
- Student stu=(Student)it.next();
- System.out.println(stu.getName()+"........"+stu.getAge());
- }
- }
- }
复制代码
不是说TreeSet存入一个学生类的话就不需要比较,运行就不会报错吗,我跟老师一样的代码,为什么老师的能运行,我存一个学生类还是会报Student cannot be cast to java.lang.Comparable |
|