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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 郭军亮 中级黑马   /  2013-5-17 18:02  /  1440 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 郭军亮 于 2013-5-18 10:22 编辑

package Collection;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetTest {

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                TreeSet ts=new TreeSet(new myCompare());
                Student t1=new Student("lisi9",23);
                Student t2=new Student("li",18);
                Student t3=new Student("lisi2",23);
                Student t4=new Student("lisi9",13);
                ts.add(t1);
                ts.add(t2);
                ts.add(t3);
                ts.add(t4);
                for(Iterator t=ts.iterator();t.hasNext();){
                        Student s=(Student)t.next();
                        System.out.println(s.getName()+"..."+s.getAge());
                }

        }

}
class Student implements Comparable{
        private String name;
        private int age;
        Student(String name,int age){
                this.name=name;
                this.age=age;
        }
        public String getName(){
                return this.name;
        }
        public int getAge(){
                return this.age;
        }
        @Override
        public int compareTo(Object o) {
                // TODO Auto-generated method stub
                if(!(o instanceof Student))
                        throw new RuntimeException("it is error!!!");
                Student s=(Student) o;
                System.out.println(this.getName()+"...compareTo..."+s.getName());
                if(this.getAge()>s.getAge())
                        return 1;
                else if(this.getAge()==s.getAge()){
                        return this.getName().compareTo(s.getName());
                }
                return -1;
                        
        }        
}
class myCompare implements Comparator{
        @Override
        public int compare(Object o1, Object o2) {
                // TODO Auto-generated method stub
                if(o1 instanceof Student && o2 instanceof Student)
                throw new RuntimeException("The type is not match!!!");//为什么加上这两句话后会发生异常呢?
                Student s1=(Student)o1;
                Student s2=(Student)o2;
                int num=s1.getName().compareTo(s2.getName());
                if(num==0)
                        return s1.getAge()-s2.getAge();        
                return num;
        }
        
}

点评

代码缺少注释,别人也不知道楼主要问什么,请楼主以后注意一下  发表于 2013-5-18 00:17

评分

参与人数 1技术分 +1 收起 理由
Sword + 1

查看全部评分

7 个回复

倒序浏览
楼主你这是想干什么?????
回复 使用道具 举报
楼主你这是想干什么?????
回复 使用道具 举报
解释都在代码里的注释上。
  1. class myCompare implements Comparator
  2. {
  3.         public int compare(Object o1, Object o2)
  4.         {
  5. //                通过反射获取o1,o2类型。打印结果是o1和o2都是Student类型
  6. //                o1,o2是父类引用指向子类对象。
  7.                 System.out.println(o1.getClass().getName());
  8.                 System.out.println(o2.getClass().getName());
  9. //                所以此处代码会执行
  10.                 if (o1 instanceof Student && o2 instanceof Student)
  11.                         throw new RuntimeException("The type is not match!!!");
  12.                 Student s1 = (Student) o1;
  13.                 Student s2 = (Student) o2;
  14.                 int num = s1.getName().compareTo(s2.getName());
  15.                 if (num == 0)
  16.                         return s1.getAge() - s2.getAge();
  17.                 return num;
  18.         }
  19. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
Sword + 1

查看全部评分

回复 使用道具 举报
if (o1 instanceof Student && o2 instanceof Student)
                        throw new RuntimeException("The type is not match!!!");
为什么这里会报错呢?还是不太明白啊
回复 使用道具 举报
本帖最后由 花开花落总相似 于 2013-5-17 23:41 编辑

if (o1 instanceof Student && o2 instanceof Student)
                        throw new RuntimeException("The type is not match!!!");
前面写错了 你改为
if(!(o1 instanceof Student) && !(o2 instanceof Student))   试一下  
回复 使用道具 举报
郭军亮 发表于 2013-5-17 20:54
if (o1 instanceof Student && o2 instanceof Student)
                        throw new RuntimeExcepti ...

throw new RuntimeException("The type is not match!!!");:L因为你自己在这里抛了异常啊!!!  你没看到抛的异常里面有"The type is not match:L这是你自己写的异常。。。。
回复 使用道具 举报
哦哦哦 ,我的错,谢谢了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马