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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. import java.util.*;


  2.         //由于Student对象不具备比较性,无法存入TreeSet集合中,所以要实现Comparable,实现compareTo方法
  3.         class Student implements Comparable        //该接口强制让Student具有比较性
  4.         {
  5.                 private String name;
  6.                 private int age;

  7.                 Student(String name,int age){
  8.                         this.name=name;
  9.                         this.age=age;
  10.                 }

  11.                 //实现compareTo方法
  12.                 public int compareTo(Object obj){
  13.                         if(!(obj instanceof Student))
  14.                                 throw new RuntimeException("不是学生对象");
  15.                         Student t=(Student)obj;
  16. System.out.println(this.name+".."+t.name);
  17.                         if(this.age>t.age)                //当调用对象年龄年龄大于比较对象时
  18.                                 return 1;
  19.                         if(this.age==t.age){        //当调用对象年龄等于比较对象时,再比较姓名是否相等
  20.                 //                return this.name.compareTo(t.name);//只有两者都相同相等时为同一元素
复制代码
我自己试着写了一些这个方法,结果方法TreeSet集合还是把相同元素写进去了。好吧,equals方法没有判断成功
然后我调用了一下compareTo内部打印方法,结果发现
小黄..小白   小黄比较1次



小黑..小白
小黑..小黄   小黑比较2次

小红..小黑
小红..小黄   小红怎么也是比较2次?她不和小白比较吗?

小白..小黑
小白..小黄   然后。。最后出生的小白也任性了?也只比较了两次,他姐姐小红和和他一模一样的孪生兄弟小白怎么不比较了?抓狂啊

求解


QQ图片20140209215253.jpg (85.99 KB, 下载次数: 32)

QQ图片20140209215253.jpg

评分

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

查看全部评分

8 个回复

倒序浏览
  1. import java.util.*;


  2.         //由于Student对象不具备比较性,无法存入TreeSet集合中,所以要实现Comparable,实现compareTo方法
  3.         class Student implements Comparable        //该接口强制让Student具有比较性
  4.         {
  5.                 private String name;
  6.                 private int age;

  7.                 Student(String name,int age){
  8.                         this.name=name;
  9.                         this.age=age;
  10.                 }

  11.                 //实现compareTo方法
  12.                 public int compareTo(Object obj){
  13.                         if(!(obj instanceof Student))
  14.                                 throw new RuntimeException("不是学生对象");
  15.                         Student t=(Student)obj;
  16.                         System.out.println(this.name+".."+t.name);
  17.                         if(this.age>t.age)                //当调用对象年龄年龄大于比较对象时
  18.                                 return 1;
  19.                         if(this.age==t.age){        //当调用对象年龄等于比较对象时,再比较姓名是否相等
  20.                         //        System.out.println(this.name+".."+t.name);
  21.                                 if(this.name.equals(t.name))
  22.                                         return 0;
  23.                         }
  24.                         return -1;        //年龄小于比较对象返回-1
  25.                 }

  26.                 String getName(){
  27.                         return name;
  28.                 }
  29.                 int getAge(){
  30.                         return age;
  31.                 }
  32.         }
  33.        
  34.         class TreeSetTest
  35.         {
  36.                 public static void main(String args[]){


  37.                
  38.                         TreeSet ts=new TreeSet();

  39.                         ts.add(new Student("小白",21));
  40.                         ts.add(new Student("小黄",20));
  41.                         ts.add(new Student("小黑",21));
  42.                         ts.add(new Student("小红",19));
  43.                         ts.add(new Student("小白",21));

  44.                         /*Exception in thread "main" java.lang.ClassCastException:
  45.                                 Student cannot be cast to java.lang.Comparable        由于Student对象没有比较性
  46.                         */
  47.                         Iterator it=ts.iterator();       
  48.                         while(it.hasNext()){        //打印
  49.                                 Student s=(Student)it.next();
  50.                                 sop(s.getName()+".."+s.getAge());
  51.                         }
  52.                        
  53.                 }
  54.                 public static void sop(Object obj){
  55.                         System.out.println(obj);
  56.                 }

  57.         }
复制代码



代码不全,重新贴一下

评分

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

查看全部评分

回复 使用道具 举报
   把compareTo方法改成这样就可以了
   //实现compareTo方法
        public int compareTo(Object obj){
                if(!(obj instanceof Student))
                        throw new RuntimeException("不是学生对象");
                Student t=(Student)obj;
                int temp = this.age-t.age;
                System.out.println(this.name+".."+t.name);
                //当调用对象年龄等于比较对象时,再比较姓名是否相等
                return temp==0 ? this.name.equals(t.name) ? 0 : -1 : temp;   
            
        }

评分

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

查看全部评分

回复 使用道具 举报
2424308 发表于 2014-2-9 23:01
把compareTo方法改成这样就可以了
   //实现compareTo方法
        public int compareTo(Object obj){

不行,问题没有被解决,小白还是被打印了两次。。。
回复 使用道具 举报
  return temp==0 ? this.name.equals(t.name) ? 0 : -1 : temp;  
这个是-1的问题,改成1就是你想要的结果,不过为什么-1不可以,这个我也不知道。

评分

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

查看全部评分

回复 使用道具 举报
2424308 发表于 2014-2-9 23:55
return temp==0 ? this.name.equals(t.name) ? 0 : -1 : temp;  
这个是-1的问题,改成1就是你想要的结果 ...

确定改成1就可以了,能解释一下这句话的意思吗,见过三元运算符,头一次见过这种返回,两个问号三个返回好复杂
回复 使用道具 举报
陈文杰 来自手机 中级黑马 2014-2-10 10:26:16
7#
equals比较只有true和false俩个值boolean型。二进制中0和1代表逻辑真假,而不是值相等。而比较器返回值为int有负数正数和0。不用equals.
回复 使用道具 举报
  return temp==0 ? this.name.equals(t.name) ? 0 : -1 : temp;
这种写法是什么意思啊 ,是
  1. if(temp == 0&&this.name.equals(t.name)){
  2.    return -1;
  3. }else{
  4.    return temp;
  5. }
复制代码

是这样的吗?以前没见过这种写法啊
回复 使用道具 举报
int temp = this.age - t.age;
return temp==0?this.name.compateTo(t.name):temp;

不是一般都这么比较的吗,用的是字符串的自然比较compateTo()方法
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马