使用集合类arrayList 与comparator实现。
Comparator称之为匿名内部类,只需使用一次,不需要多次创建对象。
another solution:
阅读(69) | 评论(0) | 转发(0) |
<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
Comparator称之为匿名内部类,只需使用一次,不需要多次创建对象。
点击(此处)折叠或打开
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- class Student {
- String name;
- String gender;
- int age;
- float gpa;
- String grade; //应设为私有变量,为方便省略之
-
- public Student(String name, String gender, int age, float gpa,String grade){
- this.name = name;
- this.gender = gender;
- this.age = age;
- this.gpa = gpa;
- this.grade = grade;
- }
- public void show() {
- System.out.println("Name: "+this.name+", gender: "+this.gender+", age: "+this.age+", GPA: "+this.gpa+", Grade: "+this.grade);
- }
- }
- public class Console {
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
-
- Comparator<Student> comparator = new Comparator<Student>() {
-
- @Override
- public int compare(Student o1, Student o2) {
- // TODO Auto-generated method stub
-
- /*
- * 设置compare函数应严密,考虑相等情况下下一考虑属性
- * 数值(int,float, double)可考虑直接相减
- * 字符串可采用compareTo函数
- */
- int answer;
- if (o1.gpa!=o2.gpa) {
- answer = (int) (o1.gpa*10-o2.gpa*10);
- }
- else {
- answer = o1.grade.compareTo(o2.grade);
- }
- System.out.println(answer);
- return answer;
- }
- };
- ArrayList<Student> students = new ArrayList<Student>();
- //float属性赋值需加f
-
- Student student1 = new Student("a", "m", 18, 3.8f, "2008");
- Student student2 = new Student("b", "f", 19, 4.5f, "2007");
- Student student3 = new Student("c", "m", 20, 4.4f, "2006");
- Student student4 = new Student("d", "f", 14, 4.2f, "2012");
- Student student5 = new Student("e", "f", 15, 4.0f, "2011");
- Student student6 = new Student("f", "f", 16, 4.0f, "2010");
- Student student7 = new Student("g", "m", 17, 4.2f, "2009");
- Student student8 = new Student("h", "m", 15, 3.9f, "2011");
- Student student9 = new Student("i", "f", 18, 3.9f, "2008");
- Student student10 = new Student("j", "m", 20, 3.7f, "2006");
- students.add(student1);
- students.add(student2);
- students.add(student3);
- students.add(student4);
- students.add(student5);
- students.add(student6);
- students.add(student7);
- students.add(student8);
- students.add(student9);
- students.add(student10);
- Collections.sort(students,comparator);
- for (Student student : students) {
- student.show();
- }
-
-
- }
-
- }
点击(此处)折叠或打开
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.SortedSet;
- import java.util.TreeSet;
- class Student implements Comparable<Student>{
- String name;
- String gender;
- int age;
- float gpa;
- String grade; //应设为私有变量,为方便省略之
- public Student(String name, String gender, int age, float gpa,String grade){
- this.name = name;
- this.gender = gender;
- this.age = age;
- this.gpa = gpa;
- this.grade = grade;
- }
- public void show() {
- System.out.println("Name: "+this.name+", gender: "+this.gender+", age: "+this.age+", GPA: "+this.gpa+", Grade: "+this.grade);
- }
- @Override
- public int compareTo(Student o) {
- // TODO Auto-generated method stub
- int answer;
- if (this.gpa!=o.gpa) {
- answer = (int) (this.gpa*10-o.gpa*10);
- }
- else {
- answer = this.grade.compareTo(o.grade);
- }
- System.out.println(answer);
- return answer;
- }
- }
- public class Console {
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- TreeSet<Student> students = new TreeSet<Student>();
- //float属性赋值需加f
- Student student1 = new Student("a", "m", 18, 3.8f, "2008");
- Student student2 = new Student("b", "f", 19, 4.5f, "2007");
- Student student3 = new Student("c", "m", 20, 4.4f, "2006");
- Student student4 = new Student("d", "f", 14, 4.2f, "2012");
- Student student5 = new Student("e", "f", 15, 4.0f, "2011");
- Student student6 = new Student("f", "f", 16, 4.0f, "2010");
- Student student7 = new Student("g", "m", 17, 4.2f, "2009");
- Student student8 = new Student("h", "m", 15, 3.9f, "2011");
- Student student9 = new Student("i", "f", 18, 3.9f, "2008");
- Student student10 = new Student("j", "m", 20, 3.7f, "2006");
- students.add(student1);
- students.add(student2);
- students.add(student3);
- students.add(student4);
- students.add(student5);
- students.add(student6);
- students.add(student7);
- students.add(student8);
- students.add(student9);
- students.add(student10);
- for (Student student : students) {
- student.show();
- }
-
-
- }
-
- }
相关热门文章
给主人留下些什么吧!~~
评论热议