Java 常用類庫 之 比較類 Comparable
阿新 • • 發佈:2018-06-23
instance 多少 一個 public 類關系 name length compare 實例
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根據學生成績排序"); //c.sortByBuble(); //Arrays.sort(students); c.sortByMerge(); 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; } } } /** 按照學生的數學成績冒泡排序從小到大排序 */ public Student[] sortByBuble() { for(int i=0;i<currentSize-1;i++) { for(int j=0;j<currentSize-i-1;j++) { Student s=students[j]; if(students[j].getMathScore()>students[j+1].getMathScore()) { students[j]=students[j+1]; students[j+1]=s; } } } return this.students; } public void sortByMerge() { //創建一個臨時數組存放分區元素 Comparable[] tempArray=students.clone(); merge_sort(students,tempArray,0,students.length); } private void merge_sort(Comparable[] array,Comparable[] tempArray, int first, int last) { if(first+1<last) { int mid=(first+last)/2; // 對左半部份排序 merge_sort(array,tempArray,first,mid); // 對有半部分排序 merge_sort(array,tempArray,mid,last); // 合並到一個臨時數組,再拷貝到array中 merge(array,tempArray,first,mid,last); } } private void merge(Comparable[] array,Comparable[] tempArray, int first, int mid,int last) { int beginLeft=first; int beginRight=mid; int k=first; while((beginLeft<mid)&&(beginRight<last)){ if(array[beginLeft].compareTo(array[beginRight])<0){ tempArray[k]=array[beginLeft]; beginLeft++; }else{ tempArray[k]=array[beginRight]; beginRight++; } k++; } while(beginLeft<mid){ tempArray[k++]=array[beginLeft++]; } while(beginRight<last){ tempArray[k++]=array[beginRight++]; } for(int i=first;i<last;i++){ array[i]=tempArray[i]; } } } 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
Java 常用類庫 之 比較類 Comparable