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

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


public class Student  implements Comparator{
        private static String name;
        private static int age;
        public Student(String name,int age){
                this.name = name;
                this.age = age;
        }
        public String getName(){
                return name;
        }
        public int getAge(){
                return age;
        }
        /*public int compareTo(Object obj){
                if(!(obj instanceof Student)){
                        throw new RuntimeException("haha");
                }
                Student s = (Student)obj;
                if(this.age>s.age){
                        return 1;
                }
                if(this.age==s.age){
                        return 0;
                }
                return -1;
        }*/
        public int compare(Object o1,Object o2){
                Student s1 = (Student)o1;
                Student s2 = (Student)o2;
                int num =s1.getName().compareTo(s2.getName());
                if(num==0){
                        if(s1.getAge()>s2.getAge()){
                                return 1;
                        }
                        if(s1.getAge()==s2.getAge()){
                                return 0;
                        }
                        return -1;
                }
                return num;
               
        }
        public static void main(String[] args){
                TreeSet ts = new TreeSet(new Student(name, age));
                ts.add(new Student("zhangsan",89));
                ts.add(new Student("zhangsan",19));
                ts.add(new Student("lisi",65));
                ts.add(new Student("wangwu",23));
                Iterator it = ts.iterator();
                while(it.hasNext()){
                        Student s = (Student)it.next();
                        System.out.println(s.getName()+"####"+s.getAge());
                }
               
               
        }
}


2 个回复

倒序浏览

TreeSet ts = new TreeSet(new Student(name, age));
请问这句是什么意思 ?
我怎么没记得有这样的构造方法啊 ?
回复 使用道具 举报
最基础的java基础都没有掌握
1:类  Student的名字和年龄怎么可以定义为static
private static String name;
private static int age;
2:类  Student应当实现接口Comparable的int compareTo(Object o)方法
而不是接口 Comparator的int compare(Object o1,Object o2)方法;
解决方案:去掉上述 两行  中 的static

这是我的代码
  1. package com.itheima;

  2. import java.util.Comparator;
  3. import java.util.Iterator;
  4. import java.util.TreeSet;

  5. public class Test10 {

  6.         /**
  7.          * @param args
  8.          * 10、声明类Student,包含3个成员变量:name、age、score,
  9.          * 创建5个对象装入TreeSet,按照成绩排序输出结果(考虑成绩相同的问题)
  10.          */
  11.         public static void main(String[] args) {
  12.                 // TODO Auto-generated method stub
  13.                 TreeSet<Student> treeSet=new TreeSet();
  14.                 //实例化5个student对象
  15.                 Student[] stuArr={new Student("赵", 20, 97),
  16.                                            new Student("钱", 22, 84),
  17.                                            new Student("孙", 21, 97),
  18.                                            new Student("李", 23, 89),
  19.                                            new Student("崔", 19, 99)};
  20.                 //将5个student对象增加到TreeSet对象中
  21.                 treeSet.add(stuArr[0]);
  22.                 treeSet.add(stuArr[1]);
  23.                 treeSet.add(stuArr[2]);
  24.                 treeSet.add(stuArr[3]);
  25.                 treeSet.add(stuArr[4]);
  26.                 Iterator it=treeSet.iterator();
  27.                 //获取treeSet的遍历器
  28.                 while(it.hasNext()){
  29.                         Student p = (Student)it.next();
  30.                           System.out.println("name = "+p.getName()+" || age = "+p.getAge()+" || age = "+p.getScore());
  31.                           }
  32.         }
  33.         static class Student implements Comparable{
  34.                 //实现Comparable 接口的compareTo(Object o)方法
  35.                 private String name;
  36.                 private int age;
  37.                 private float score;
  38.                 public Student(String n,int a,float s) {
  39.                         // TODO Auto-generated constructor stub
  40.                         name=n;
  41.                         age=a;
  42.                         score=s;
  43.                 }
  44.                 public String getName() {
  45.                         return name;
  46.                 }
  47.                 public void setName(String name) {
  48.                         this.name = name;
  49.                 }
  50.                 public int getAge() {
  51.                         return age;
  52.                 }
  53.                 public void setAge(int age) {
  54.                         this.age = age;
  55.                 }
  56.                 public float getScore() {
  57.                         return score;
  58.                 }
  59.                 public void setScore(float score) {
  60.                         this.score = score;
  61.                 }
  62.                 @Override
  63.                 public int compareTo(Object o) {
  64.                         // TODO Auto-generated method stub
  65.                         Student stu=(Student) o;
  66.                         //以下实现的是升序排列
  67.                         if(this.score>stu.score){
  68.                                 return 1;
  69.                         }
  70.                         if(this.score<stu.score){
  71.                                 return -1;
  72.                         }
  73.                         return 0;
  74.                 }
  75.         }
  76. }
复制代码

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