黑马程序员技术交流社区

标题: TreeSet添加自定义对象问题?能添加相同的两个人 求解 [打印本页]

作者: 山鹰    时间: 2013-6-4 18:10
标题: TreeSet添加自定义对象问题?能添加相同的两个人 求解
本帖最后由 Sword 于 2013-6-6 18:54 编辑

知道是重写接口方法那得的问题,改了好久改不过来,求解
public class Student implements Comparable{
        
        private String name;
        private int age;
        
        public Student(){};
        
        public Student(String name,int age){
                this.name = name;
                this.age = age;
        }
        public String getName(){
                return name;
        }
        public void setName(String name){
                this.name = name ;
        }
        public int getAge(){
                return age;
        }
        public void setAge(int age){
                this.age = age;
        }
//重写comparable接口的方法:
        public int compareTo(Object obj) {
                if(!(obj instanceof Student)){
                        Student s = (Student)obj;
                        if(this.age > s.age){
                                return 1;
                        }else if(this.age == s.age){
                                return this.name.compareTo(s.name);
                        }else{
                                return -1;
                        }
                }
                return age ;  //应该是这的问题,毕老师的视频中if语句都没加{}我在MyEslise中加上就乱了  求解
        }
}
public class TreeSetDemo {
        
        public static void main(String[] args) {
               
                TreeSet ts = new TreeSet();
               
                ts.add(new Student("张三",20));
                ts.add(new Student("李四",22));
                ts.add(new Student("王五",24));
                ts.add(new Student("赵六",26));
                ts.add(new Student("赵六",26));
               
                for(Iterator it = ts.iterator(); it.hasNext();){
                        Student s = (Student)it.next();//向下转型
                        System.out.println(s.getName() + "-->" + s.getAge());
                        
                }
        }

}

作者: 娄田田    时间: 2013-6-4 18:44
  1. // 重写comparable接口的方法:
  2.         public int compareTo(Object obj) {
  3.                 System.out.println(!(obj instanceof Student));
  4.                 if (obj instanceof Student) {
  5.                         Student s = (Student) obj;
  6.                         if (this.age > s.age) {
  7.                                 return 1;
  8.                         } else if (this.age == s.age) {
  9.                                 return this.name.compareTo(s.name);
  10.                         } else {
  11.                                 return -1;
  12.                         }
  13.                 }
  14.                 return age;
  15.         }
复制代码
把那行判断是不是Student的实例前的!去掉就行了,因为你传入的都是Student的实例,就是说System.out.println(!(obj instanceof Student));打印出来为false了,改一下就好了。
作者: 风乐    时间: 2013-6-4 18:47
本帖最后由 风乐 于 2013-6-4 18:48 编辑

//知道是重写接口方法那得的问题,改了好久改不过来,求解

public class Student implements Comparable{
        
        private String name;
        private int age;
        
        public Student(){};
        
        public Student(String name,int age){
                this.name = name;
                this.age = age;
        }
        public String getName(){
                return name;
        }
        public void setName(String name){
                this.name = name ;
        }
        public int getAge(){
                return age;
        }
        public void setAge(int age){
                this.age = age;
        }
//重写comparable接口的方法:
        public int compareTo(Object obj) {
                        //这里写错了,不该加那个!,否则传入的student对象全不符合if条件,if内语句不执行,直接返回age,是非0数,肯定无法保证唯一了
                if(!(obj instanceof Student)){
                        Student s = (Student)obj;
                        if(this.age > s.age){
                                return 1;
                        }else if(this.age == s.age){
                                return this.name.compareTo(s.name);
                        }else{
                                return -1;
                        }
                }
                return age ;  //应该是这的问题,毕老师的视频中if语句都没加{}我在MyEslise中加上就乱了  求解
        }
}
public class TreeSetDemo {
        
        public static void main(String[] args) {
               
                TreeSet ts = new TreeSet();
               
                ts.add(new Student("张三",20));
                ts.add(new Student("李四",22));
                ts.add(new Student("王五",24));
                ts.add(new Student("赵六",26));
                ts.add(new Student("赵六",26));
               
                for(Iterator it = ts.iterator(); it.hasNext();){
                        Student s = (Student)it.next();//向下转型
                        System.out.println(s.getName() + "-->" + s.getAge());
                        
                }
        }

}

作者: 山鹰    时间: 2013-6-4 20:39
风乐 发表于 2013-6-4 18:47
//知道是重写接口方法那得的问题,改了好久改不过来,求解

public class Student implements Comparable{

解决了
作者: Sword    时间: 2013-6-6 11:23
问题已解决的话,请及时修改分类,谢谢合作!
作者: 山鹰    时间: 2013-6-6 18:32
Sword 发表于 2013-6-6 11:23
问题已解决的话,请及时修改分类,谢谢合作!

在哪里修改  原来重新编辑就能修改, 现在我试了试 不可以重新编辑,   该怎么修改
作者: Sword    时间: 2013-6-6 18:56
山鹰 发表于 2013-6-6 18:32
在哪里修改  原来重新编辑就能修改, 现在我试了试 不可以重新编辑,   该怎么修改 ...

已经帮你修改了,还是原来的方法,可以的





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2