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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 911288373 中级黑马   /  2012-12-20 20:53  /  1092 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class TreeSetDemo1
{
        public static void main(String[] args)
        {
                TreeSet ts = new TreeSet();

                ts.add(new Student("lisi07",22));
                ts.add(new Student("lisi05",18));
                ts.add(new Student("lisi02",20));
                ts.add(new Student("lisi04",2));
                ts.add(new Student("lisi03",29));
                ts.add(new Student("lisi09",52));

                Iterator it = ts.iterator();
                while(it.hasNext())
                {
                        Student stu = (Student)it.next();
                        System.out.println(stu.getName()+"..."+stu.getAge());
                }
        }
}

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)
        {
                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;
        }
}


//错误是在37行。 请帮忙改正 多谢。

评分

参与人数 1技术分 +1 收起 理由
奋斗的青春 + 1 鼓励分 。

查看全部评分

5 个回复

倒序浏览
本帖最后由 王小聪 于 2012-12-20 21:20 编辑

throw new RuntimeException;
//加个分号可以吗

评分

参与人数 1黑马币 +6 收起 理由
911288373 + 6 赞一个!

查看全部评分

回复 使用道具 举报
你的get(),set()方法在哪儿啊?
回复 使用道具 举报
少了两个方法
public String getName()
{
    return name;

}
public int getAge()
{
    return age;
}
       

评分

参与人数 2技术分 +1 黑马币 +10 收起 理由
911288373 + 10 很给力!
奋斗的青春 + 1 赞一个!

查看全部评分

回复 使用道具 举报
高境 发表于 2012-12-20 21:15
少了两个方法
public String getName()
{

嗯。  对劲!
   马虎了

  谢谢大哥
回复 使用道具 举报
王小聪 发表于 2012-12-20 21:12
throw new RuntimeException;
//加个分号可以吗

多谢兄弟
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马