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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 宋耀冬 中级黑马   /  2013-4-5 13:13  /  1040 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 宋耀冬 于 2013-4-5 17:48 编辑
  1. import java.util.*;

  2. class TreeSetDemo {
  3.         public static void main (String[] args) {
  4.                 TreeSet ts = new TreeSet();

  5.                 ts.add(new Student("lisi02", 22));
  6.                 ts.add(new Student("lisi007", 20));
  7.                 ts.add(new Student("lisi09", 19));
  8.                 ts.add(new Student("lisi08", 19));
  9.                 //ts.add(new Student("lisi01", 40));

  10.                 Iterator it = ts.iterator();

  11.                 while(it.hasNext()) {
  12.                         Student stu = (Student)it.next();
  13.                         System.out.println(stu.getName()+"..."+stu.getAge());
  14.                 }
  15.         }
  16. }

  17. class Student implements Comparable {
  18.         private String name;
  19.         private int age;

  20.         Student (String name, int age) {
  21.                 this.name = name;
  22.                 this.age = age;
  23.         }

  24.         public int compareTo(Object obj) {
  25.                 if(!(obj instanceof Student))
  26.                         throw new RuntimeException("不是学生对象");
  27.                 Student s = (Student)obj;
  28.                
  29.                 System.out.println(this.name+"...compareto..."+s.name);
  30.                 if(this.age>s.age)
  31.                         return 1;
  32.                 if(this.age==s.age){
  33.                         return this.name.compareTo(s.name);
  34.                 }
  35.                         
  36.                 return -1;
  37.         }

  38.         public String getName() {
  39.                 return name;
  40.         }

  41.         public int getAge() {
  42.                 return age;
  43.         }
  44. }
复制代码
我的运行结果是这样的
lisi02...compareto...lisi02
lisi007...compareto...lisi02
lisi09...compareto...lisi02
lisi09...compareto...lisi007
lisi08...compareto...lisi007
lisi08...compareto...lisi09
lisi08...19
lisi09...19
lisi007...20
lisi02...22

lisi02 自身进行了一次比较  老师的视频上没有啊  困惑 求解

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

6 个回复

倒序浏览
运行结果是  lisi007....compareto.....lisi02
lisi09....compareto.....lisi02
lisi09....compareto.....lisi007
lisi08....compareto.....lisi007
lisi08....compareto.....lisi09
lisi08...19
lisi09...19
lisi007...20
lisi02...22



你重新运行下试试、、
回复 使用道具 举报
import java.util.*;

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

                ts.add(new Student("lisi02", 22));
                ts.add(new Student("lisi007", 20));
                ts.add(new Student("lisi09", 19));
                ts.add(new Student("lisi08", 19));
                //ts.add(new Student("lisi01", 40));

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

        public String getName() {
                return name;
        }

        public int getAge() {
                return age;
        }
}


我的运行结果是
lisi007....compareto.....lisi02
lisi09....compareto.....lisi02
lisi09....compareto.....lisi007
lisi08....compareto.....lisi007
lisi08....compareto.....lisi09
lisi08...19
lisi09...19
lisi007...20
lisi02...22

你在运行一次试试
回复 使用道具 举报
运行了一下,跟上面两位的结果一样,是不是LZ多加了一行        ts.add(new Student("lisi02", 22));     
回复 使用道具 举报
本帖最后由 lcjasas 于 2013-4-5 15:33 编辑


我的输出结果也会多出lisi02....compareto.....lisi02
不知道是不是jdk版本不一样的原因。
回复 使用道具 举报
我用的JDK 1.7  你们呢
回复 使用道具 举报
lisi007...compareto...lisi02
lisi09...compareto...lisi02
lisi09...compareto...lisi007
lisi08...compareto...lisi007
lisi08...compareto...lisi09
lisi08...19
lisi09...19
lisi007...20
lisi02...22

我的运行结果是这样的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马