Chinaunix首页 | 论坛 | 博客
  • 博客访问: 83041
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 839
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-12 14:38
个人简介

我在收集一些石头,我期待它们有一天能够串联成一串项链。

文章分类

全部博文(31)

文章存档

2013年(31)

我的朋友

分类: Java

2013-03-12 14:55:30

使用集合类arrayList 与comparator实现。
Comparator称之为匿名内部类,只需使用一次,不需要多次创建对象。

点击(此处)折叠或打开

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. class Student {
  5.      String name;
  6.      String gender;
  7.      int age;
  8.      float gpa;
  9.      String grade; //应设为私有变量,为方便省略之

  10.     public Student(String name, String gender, int age, float gpa,String grade){
  11.         this.name = name;
  12.         this.gender = gender;
  13.         this.age = age;
  14.         this.gpa = gpa;
  15.         this.grade = grade;
  16.     }
  17.     public void show() {
  18.         System.out.println("Name: "+this.name+", gender: "+this.gender+", age: "+this.age+", GPA: "+this.gpa+", Grade: "+this.grade);
  19.     }
  20. }
  21. public class Console {

  22.     /**
  23.      * @param args
  24.      */
  25.     public static void main(String[] args) {
  26.         // TODO Auto-generated method stub

  27.         Comparator<Student> comparator = new Comparator<Student>() {

  28.             @Override
  29.             public int compare(Student o1, Student o2) {
  30.                 // TODO Auto-generated method stub

  31.                 /*
  32.                  * 设置compare函数应严密,考虑相等情况下下一考虑属性
  33.                  * 数值(int,float, double)可考虑直接相减
  34.                  * 字符串可采用compareTo函数
  35.                  */
  36.                 int answer;
  37.                 if (o1.gpa!=o2.gpa) {
  38.                     answer = (int) (o1.gpa*10-o2.gpa*10);
  39.                 }
  40.                 else {
  41.                     answer = o1.grade.compareTo(o2.grade);
  42.                 }
  43.                 System.out.println(answer);
  44.                 return answer;
  45.             }
  46.         };
  47.         ArrayList<Student> students = new ArrayList<Student>();
  48.         //float属性赋值需加f

  49.         Student student1 = new Student("a", "m", 18, 3.8f, "2008");
  50.         Student student2 = new Student("b", "f", 19, 4.5f, "2007");
  51.         Student student3 = new Student("c", "m", 20, 4.4f, "2006");
  52.         Student student4 = new Student("d", "f", 14, 4.2f, "2012");
  53.         Student student5 = new Student("e", "f", 15, 4.0f, "2011");
  54.         Student student6 = new Student("f", "f", 16, 4.0f, "2010");
  55.         Student student7 = new Student("g", "m", 17, 4.2f, "2009");
  56.         Student student8 = new Student("h", "m", 15, 3.9f, "2011");
  57.         Student student9 = new Student("i", "f", 18, 3.9f, "2008");
  58.         Student student10 = new Student("j", "m", 20, 3.7f, "2006");
  59.         students.add(student1);
  60.         students.add(student2);
  61.         students.add(student3);
  62.         students.add(student4);
  63.         students.add(student5);
  64.         students.add(student6);
  65.         students.add(student7);
  66.         students.add(student8);
  67.         students.add(student9);
  68.         students.add(student10);
  69.         Collections.sort(students,comparator);
  70.         for (Student student : students) {
  71.             student.show();
  72.         }
  73.         
  74.         
  75.     }

  76. }
another solution:
    

点击(此处)折叠或打开

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.SortedSet;
  5. import java.util.TreeSet;
  6. class Student implements Comparable<Student>{
  7.      String name;
  8.      String gender;
  9.      int age;
  10.      float gpa;
  11.      String grade; //应设为私有变量,为方便省略之
  12.     public Student(String name, String gender, int age, float gpa,String grade){
  13.         this.name = name;
  14.         this.gender = gender;
  15.         this.age = age;
  16.         this.gpa = gpa;
  17.         this.grade = grade;
  18.     }
  19.     public void show() {
  20.         System.out.println("Name: "+this.name+", gender: "+this.gender+", age: "+this.age+", GPA: "+this.gpa+", Grade: "+this.grade);
  21.     }
  22.     @Override
  23.     public int compareTo(Student o) {
  24.         // TODO Auto-generated method stub
  25.         int answer;
  26.         if (this.gpa!=o.gpa) {
  27.             answer = (int) (this.gpa*10-o.gpa*10);
  28.         }
  29.         else {
  30.             answer = this.grade.compareTo(o.grade);
  31.         }
  32.         System.out.println(answer);
  33.         return answer;
  34.     }
  35. }
  36. public class Console {

  37.     /**
  38.      * @param args
  39.      */
  40.     public static void main(String[] args) {
  41.         // TODO Auto-generated method stub
  42.         TreeSet<Student> students = new TreeSet<Student>();
  43.         //float属性赋值需加f
  44.         Student student1 = new Student("a", "m", 18, 3.8f, "2008");
  45.         Student student2 = new Student("b", "f", 19, 4.5f, "2007");
  46.         Student student3 = new Student("c", "m", 20, 4.4f, "2006");
  47.         Student student4 = new Student("d", "f", 14, 4.2f, "2012");
  48.         Student student5 = new Student("e", "f", 15, 4.0f, "2011");
  49.         Student student6 = new Student("f", "f", 16, 4.0f, "2010");
  50.         Student student7 = new Student("g", "m", 17, 4.2f, "2009");
  51.         Student student8 = new Student("h", "m", 15, 3.9f, "2011");
  52.         Student student9 = new Student("i", "f", 18, 3.9f, "2008");
  53.         Student student10 = new Student("j", "m", 20, 3.7f, "2006");
  54.         students.add(student1);
  55.         students.add(student2);
  56.         students.add(student3);
  57.         students.add(student4);
  58.         students.add(student5);
  59.         students.add(student6);
  60.         students.add(student7);
  61.         students.add(student8);
  62.         students.add(student9);
  63.         students.add(student10);
  64.         for (Student student : students) {
  65.             student.show();
  66.         }
  67.         
  68.         
  69.     }

  70. }

阅读(740) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:equal | == | equals | hashCode

给主人留下些什么吧!~~