Java 常用類庫 之 比較類 Comparable

http://www.verejava.com/?id=169930999133100

/**
    知識點: 比較類 Comparable

    題目: 將某班學生按數學成績從小到大排序

    思路:
        1. 抽象出類:
            1.1 班級(ClassSet)
            1.2 學生(Student)
        2. 找出類關係:
            2.1 學生 屬於 班級 Student -> ClassSet(多對1)
        3. 找出類屬性:
            3.1 ClassSet(班級名稱,班級人數)
            3.2 Student(學生名稱,數學成績)
        4. 找出類方法:
            4.1 學生添加到班級 ClassSet{addStudent(Student s)}
            4.2 學生成績從小到大排序  ClassSet{sortByScore()}
*/
import java.util.Arrays;

public class TestComparable {
    
    public static void main(String[] args) {
        //實例化4G班級
        ClassSet c = new ClassSet("4G", 4);
        //添加學生
        c.addStudent(new Student("李明", 90));
        c.addStudent(new Student("李浩", 80));
        c.addStudent(new Student("王濤", 95));
        c.addStudent(new Student("張勝", 70));

        //獲得4G班級學生數組集合
        Student[] students = c.getStudents();
        //輸出學生信息
        for (Student s : students) {
            if (s != null)
                System.out.println(s.getName() + "," + s.getMathScore());
        }

        System.out.println("\n根據學生成績排序");
        Arrays.sort(students);
        for (Student s : students) {
            if (s != null)
                System.out.println(s.getName() + "," + s.getMathScore());
        }

    }
}

class ClassSet {
    
    private String className;//班級名稱
    private int maxSize;//班級學生人數
    private int currentSize;//當前多少學生
    private Student[] students;//所有學生的數組

    public ClassSet(String className, int maxSize) {
        this.className = className;
        this.maxSize = maxSize;
        students = new Student[maxSize];
    }

    public Student[] getStudents() {
        return this.students;
    }

    //添加學生
    public void addStudent(Student s) {
        for (int i = 0; i < students.length; i++) {
            if (students[i] == null) {
                students[i] = s;
                currentSize++;
                break;
            }
        }
    }
}

class Student implements Comparable {
    private String name;//學生姓名
    private int mathScore;//數學成績

    public Student(String name, int mathScore) {
        this.name = name;
        this.mathScore = mathScore;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMathScore() {
        return this.mathScore;
    }

    public void setMathScore(int mathScore) {
        this.mathScore = mathScore;
    }

    /**
        實現Comparable 接口要複寫 compareTo(T o) 方法
        如果從小到大排序
        大於         則返回 1
        小於         則返回 -1
        等於         則返回 0
        如果從大到小排序
        大於         則返回 -1
        小於         則返回 1
        等於         則返回 0
    */
    public int compareTo(Object obj) {
        if (obj instanceof Student) {
            Student s = (Student) obj;
            if (this.mathScore > s.getMathScore())
                return 1;
            if (this.mathScore < s.getMathScore())
                return -1;

        }
        return 0;
    }

}

http://www.verejava.com/?id=169930999133100

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章