本帖最后由 魏志雷 于 2012-2-1 14:16 编辑
public class TreeSetDemo
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new Student("java02", 22));
// ts.add(new Student("java02",20));
// ts.add(new Student("java03",19));
// ts.add(new Student("java04",40));
Iterator it = ts.iterator();
while (it.hasNext())
{
Student stu=(Student)it.next();
sop(stu.getName()+"::"+stu.getAge());
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
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;
}
}
这段代码为什么会抛出ClassCastException异常,视频上面讲解,存储一个对象是不会抛出异常的,检查好几遍没有发现什么原因! |
|