前幾天java考試,遇到對對象數組進行排序,不太理解其中的原理,下面來更深入的理解下。(具體sort()的使用方法參考API文檔)Arrays.sort()的使用主要分為對基礎資料型別 (Elementary Data Type)數組的排序和對對象數組的排序.
1.對基礎資料型別 (Elementary Data Type)數組的排序
1>數字排序:
int[] intArray = new int[]{1,56,-5,33};
Arrays.sort(intArray);
System.out.println(Arrays.toString(intArray));
2>字串排序(先大寫後小寫):
String[] strArray = new String[]{"Z", "a", "D"};
Arrays.sort(strArray);
System.out.println(Arrays.toString(strArray));
3>字串字母表排序(忽略大小寫)
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
4>反向排序
Arrays.sort(strArray, Collections.reverseOrder());
說明:
1>Arrays.sort()使用的是“經過調優的快速排序法
2>比如int[],double[],char[]等基資料類型的數組,Arrays類之只是提供了預設的升序排列,沒有提供相應的降序排列方法
2.對對象數組的排序
要實現對對象數組的排序,要先實現Comparable或者Comparator介面。下面先簡單瞭解以下這兩個介面以及它們的差別:
1> Comparable 是排序介面,若一個類實現了Comparable介面,就意味著“該類支援排序”。 即然實現Comparable介面的類支援排序,假設現在存在“實現Comparable介面的類的對象的List列表(或數組)”,則該List列表(或數組)可以通過 Collections.sort(或 Arrays.sort)進行排序。
Comparable 介面僅僅只包括一個函數,它的定義如下:
package java.lang;
import java.util.*;
public interface Comparable<T> {
public int compareTo(T o);
}
說明:
假設我們通過 x.compareTo(y) 來“比較x和y的大小”。若返回“負數”,意味著“x比y小”;返回“零”,意味著“x等於y”;返回“正數”,意味著“x大於y”。
2> Comparator 是比較子介面,我們若需要控制某個類的次序,而該類本身不支援排序(即沒有實現Comparable介面);那麼,我們可以建立一個“該類的比較子”來進行排序。這個“比較子”只需要實現Comparator介面即可。
Comparator 介面僅僅只包括兩個個函數,它的定義如下:
package java.util;public interface Comparator<T> { int compare(T o1, T o2); boolean equals(Object obj);}
說明:
(1) 若一個類要實現Comparator介面:它一定要實現compareTo(T o1,T o2) 函數,但可以不實現 equals(Object obj) 函數。
為什麼可以不實現 equals(Object obj) 函數呢。 因為任何類,預設都是已經實現了equals(Object obj)的。 Java中的一切類都是繼承於java.lang.Object,在Object.java中實現了equals(Object obj)函數;所以,其它所有的類也相當雩都實現了該函數。
(2) int compare(T o1, T o2) 是“比較o1和o2的大小”。返回“負數”,意味著“o1比o2小”;返回“零”,意味著“o1等於o2”;返回“正數”,意味著“o1大於o2”。
(3) 這個排序演算法是“經過調優的合并排序”演算法
我們不難發現:Comparable相當於“內部比較子”,而Comparator相當於“外部比較子”。
下面通到期中考試的試題來具體實現兩個介面:
鍵盤輸入兩個整數m,n。m作為隨機數的種子,產生n個學生的高數、物理、英語成績(成績均為整數,大於等於0小於等於100),學號從1開始按序遞增到n。請按總成績由低到高排序輸出。
import java.util.Comparator;import java.util.Random;import java.util.Scanner;/** * Created by hexing on 15-5-20. *///實現Comparable介面class Student1 implements Comparable<Student1>{ public int id,grad1, grad2, grad3,sum; public int compareTo(Student1 o){ if(this.sum>o.sum) { return 1; }else if(this.sum<o.sum) return -1; else return 0; }}//實現Comparator介面class Student2 implements Comparator<Student2>{ public int id,grad1, grad2, grad3,sum; public int compare(Student2 o1, Student2 o2){ return o1.sum-o2.sum; }}public class test1 { public static void main(String[] args) { int m, n; System.out.println("請輸入m,n:"); Scanner reader = new Scanner(System.in); m = reader.nextInt(); n = reader.nextInt(); Random r = new Random(m); Student1[] s1 = new Student1[n]; for(int i = 0; i < n; i++){ s1[i] = new Student1(); s1[i].id = i+1; s1[i].grad1 = r.nextInt(101); s1[i].grad2 = r.nextInt(101); s1[i].grad3 = r.nextInt(101); s1[i].sum = s1[i].grad1+s1[i].grad2+s1[i].grad3; } Arrays.sort(s1); System.out.println("Comparable介面實現的排序:"); for (int i = 0; i < s1.length; i++){ System.out.printf("%5d%5d%5d%5d%5d\n",s1[i].id,s1[i].grad1,s1[i].grad2,s1[i].grad3,s1[i].sum); } Student2[] s = new Student2[n]; for(int i = 0; i < n; i++){ s[i] = new Student2(); s[i].id = i+1; s[i].grad1 = r.nextInt(101); s[i].grad2 = r.nextInt(101); s[i].grad3 = r.nextInt(101); s[i].sum = s[i].grad1+s[i].grad2+s[i].grad3; } Arrays.sort(s, new Student2()); System.out.println("Comparator介面實現的排序:"); for (int i = 0; i < s.length; i++){ System.out.printf("%5d%5d%5d%5d%5d\n",s[i].id,s[i].grad1,s[i].grad2,s[i].grad3,s[i].sum); } }}