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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 cat73 于 2014-7-23 23:20 编辑

代码可能略显乱 最下面的Test10.run()是测试代码
实现了对学生类的排序 并且可以定义哪个当主要哪个当次要条件以及升序还是降序


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

  4. /**
  5. * 一个简单的学生类
  6. * @author Cat73
  7. */
  8. class Student{
  9.         private String name;
  10.         private int age;
  11.         private float exam;
  12.         
  13.         /**
  14.          * 构造一个学生类并定义好姓名年龄成绩
  15.          * @param name 姓名
  16.          * @param age 年龄
  17.          * @param exam 成绩
  18.          */
  19.         Student(String name, int age, float exam){
  20.                 this.name = name;
  21.                 this.age = age;
  22.                 this.exam = exam;
  23.         }
  24.         
  25.         public String getName(){
  26.                 return name;
  27.         }
  28.         
  29.         public int getAge(){
  30.                 return age;
  31.         }
  32.         
  33.         public float getExam(){
  34.                 return exam;
  35.         }
  36.         
  37.         public String getInfo(){
  38.                 return "name:" + name + ", age:" + age + ", exam:" + exam;
  39.         }
  40. }

  41. class StudentCollection implements Comparator<Student>{
  42.         
  43.         /**
  44.          * 排序方式:按照姓名
  45.          */
  46.         public static final int CONDITIONS_NAME = 0;
  47.         /**
  48.          * 排序方式:按照年龄
  49.          */
  50.         public static final int CONDITIONS_AGE = 1;
  51.         /**
  52.          * 排序方式:按照成绩
  53.          */
  54.         public static final int CONDITIONS_EXAM = 2;
  55.         
  56.         //定义3个排序条件
  57.         private int conditions1, conditions2, conditions3;
  58.         //定义一个变量记录是否为升序
  59.         private int ascending;
  60.         
  61.         /**
  62.          * 构造一个排序类对Student进行排序, 可以指定排序条件
  63.          * @param conditions1 主排序条件 本类中以"CONDITIONS_"开头的常量
  64.          * @param conditions2 次要排序条件 本类中以"CONDITIONS_"开头的常量
  65.          * @param ascending 是否为升序排序
  66.          * @throws RuntimeException 当主要与次要排序条件相同或非法时
  67.          */
  68.         public StudentCollection(int conditions1, int conditions2, boolean ascending) throws RuntimeException{
  69.                 if(conditions1 == conditions2){
  70.                         throw new RuntimeException("两个排序条件不可以相同!");
  71.                 }
  72.                
  73.                 if(conditions1 > 2 || conditions1 < 0 || conditions2 > 2 || conditions2 < 0){
  74.                         throw new RuntimeException("排序条件值非法!");
  75.                 }
  76.                
  77.                 this.conditions1 = conditions1;
  78.                 this.conditions2 = conditions2;
  79.                 this.conditions3 = (3 - conditions1 - conditions2);
  80.                 this.ascending = ascending ? 1 : -1;
  81.         }
  82.         
  83.         /**
  84.          * 比较两个学生对象,基于构造时指定的顺序
  85.          */
  86.         public int compare(Student o1, Student o2) {
  87.                 int num = compare(conditions1, o1, o2);
  88.                 if(num == 0){
  89.                         num = compare(conditions2, o1, o2);
  90.                         if(num == 0){
  91.                                 num = compare(conditions3, o1, o2);
  92.                         }
  93.                 }
  94.                
  95.                 return num * ascending;
  96.         }
  97.         
  98.         /**
  99.          * 使用某个特定的条件进行比较
  100.          * @param conditions 条件
  101.          * @return 比较结果
  102.          */
  103.         private int compare(int conditions, Student o1, Student o2){
  104.                 int num = 0;
  105.                 switch(conditions){
  106.                
  107.                 case CONDITIONS_NAME:
  108.                         num = o1.getName().compareTo(o2.getName());
  109.                         break;
  110.                 case CONDITIONS_AGE:
  111.                         num = o1.getAge() - o2.getAge();
  112.                         break;
  113.                 case CONDITIONS_EXAM:
  114.                         num = (int)(o1.getExam() - o2.getExam());
  115.                         break;
  116.                 }
  117.                 return num;
  118.         }
  119.         
  120. }

  121. public class Test10 {
  122.         public static void run(){
  123.                
  124.                 //定义一个比较器来排序学生, 排序方式为有限比较成绩, 其次姓名, 最后比较年龄, 结果按升序排序
  125.                 StudentCollection sc = new StudentCollection(
  126.                                 StudentCollection.CONDITIONS_EXAM, StudentCollection.CONDITIONS_NAME, true);
  127.                
  128.                 //定义一个TreeSet来存储学生对象 并使用自定义的排序器进行排序
  129.                 TreeSet<Student> t = new TreeSet<>(sc);
  130.                 sc = null;
  131.                
  132.                 t.add(new Student("a1", 10, 50));
  133.                 t.add(new Student("bb", 10, 50));//成绩相同姓名不同
  134.                 t.add(new Student("a1", 11, 50));//成绩相同姓名相同年龄不同
  135.                 t.add(new Student("jason", 10, 99));
  136.                 t.add(new Student("zhangsan", 10, 9));
  137.                
  138.                 Iterator<Student> it = t.iterator();
  139.                 while(it.hasNext()){
  140.                         System.out.println(it.next().getInfo());
  141.                 }
  142.         }
  143. }
复制代码



输出结果
  1. name:zhangsan, age:10, exam:9.0
  2. name:a1, age:10, exam:50.0
  3. name:a1, age:11, exam:50.0
  4. name:bb, age:10, exam:50.0
  5. name:jason, age:10, exam:99.0
复制代码




0 个回复

您需要登录后才可以回帖 登录 | 加入黑马