Arrays.sort(T[],Comparator<? super T>c)是一種用于對(duì)用戶定義的對(duì)象數(shù)組進(jìn)行排序的方法。官方的Java Doc 簡(jiǎn)單地描述它做了什么乎完,但不是深入立即揉抵。在這篇文章中亡容,我將介紹關(guān)鍵信息,以便更深入地介紹這種方法冤今。
1.如何用Arrays.sort():一個(gè)簡(jiǎn)單例子
通過(guò)閱讀下面的例子闺兢,你可以對(duì)如何使用這個(gè)方法有個(gè)正確的認(rèn)識(shí)。定義一個(gè)比較器戏罢,用于按照狗的尺寸來(lái)比較大小屋谭,然后比較器用排序方法的參數(shù)。
import java.util.Arrays;
import java.util.Comparator;
class Dog{
int size;
public Dog(int s){
size = s;
}
}
class DogSizeComparator implements Comparator<Dog>{
@Override
public int compare(Dog o1, Dog o2) {
return o1.size - o2.size;
}
}
public class ArraySort {
public static void main(String[] args) {
Dog d1 = new Dog(2);
Dog d2 = new Dog(1);
Dog d3 = new Dog(3);
Dog[] dogArray = {d1, d2, d3};
printDogs(dogArray);
Arrays.sort(dogArray, new DogSizeComparator());
printDogs(dogArray);
}
public static void printDogs(Dog[] dogs){
for(Dog d: dogs)
System.out.print(d.size + " " );
System.out.println();
}
}
輸出:
2 1 3
1 2 3
2.Arrays.sort()中使用的策略模式
這是一個(gè)很好使用策略模式的例子龟糕,值得注意的是為什么策略模式在這里會(huì)比較適合桐磁。簡(jiǎn)而言之,策略模式準(zhǔn)許在運(yùn)行時(shí)候選擇不同的算法讲岁。在這種情況下我擂,通過(guò)傳遞不同的比較器,可以選擇不同的算法缓艳⌒DΓ基于上面的例子,假若你有另外一個(gè)比較器阶淘,比較狗的重量而不是大小衙吩,你可以簡(jiǎn)單地創(chuàng)建一個(gè)新的比較器,如下:
class Dog{
int size;
int weight;
public Dog(int s, int w){
size = s;
weight = w;
}
}
class DogSizeComparator implements Comparator<Dog>{
@Override
public int compare(Dog o1, Dog o2) {
return o1.size - o2.size;
}
}
class DogWeightComparator implements Comparator<Dog>{
@Override
public int compare(Dog o1, Dog o2) {
return o1.weight - o2.weight;
}
}
public class ArraySort {
public static void main(String[] args) {
Dog d1 = new Dog(2, 50);
Dog d2 = new Dog(1, 30);
Dog d3 = new Dog(3, 40);
Dog[] dogArray = {d1, d2, d3};
printDogs(dogArray);
Arrays.sort(dogArray, new DogSizeComparator());
printDogs(dogArray);
Arrays.sort(dogArray, new DogWeightComparator());
printDogs(dogArray);
}
public static void printDogs(Dog[] dogs){
for(Dog d: dogs)
System.out.print("size="+d.size + " weight=" + d.weight + " ");
System.out.println();
}
}
size=2 weight=50 size=1 weight=30 size=3 weight=40
size=1 weight=30 size=2 weight=50 size=3 weight=40
size=1 weight=30 size=3 weight=40 size=2 weight=50
Comparator 僅僅是個(gè)接口溪窒,任何實(shí)現(xiàn)這個(gè)接口的比較器都可以在運(yùn)行時(shí)候被使用分井。這是策略模式的關(guān)鍵思想。
3.為什么用"Super"
如果直接用Comparator<T> c 作為參數(shù)霉猛,第二個(gè)參數(shù)是"Comparator<? super T> c" <? super T>意味這類型可以是T或者它的父類型尺锚。為什么準(zhǔn)許父類型。答案是:
這種方法準(zhǔn)許為所有的子類使用相同的比較器惜浅。以下的示例是明顯的:
import java.util.Arrays;
import java.util.Comparator;
class Animal{
int size;
}
class Dog extends Animal{
public Dog(int s){
size = s;
}
}
class Cat extends Animal{
public Cat(int s){
size = s;
}
}
class AnimalSizeComparator implements Comparator<Animal>{
@Override
public int compare(Animal o1, Animal o2) {
return o1.size - o2.size;
}
//in this way, all sub classes of Animal can use this comparator.
}
public class ArraySort {
public static void main(String[] args) {
Dog d1 = new Dog(2);
Dog d2 = new Dog(1);
Dog d3 = new Dog(3);
Dog[] dogArray = {d1, d2, d3};
printDogs(dogArray);
Arrays.sort(dogArray, new AnimalSizeComparator());
printDogs(dogArray);
System.out.println();
//when you have an array of Cat, same Comparator can be used.
Cat c1 = new Cat(2);
Cat c2 = new Cat(1);
Cat c3 = new Cat(3);
Cat[] catArray = {c1, c2, c3};
printDogs(catArray);
Arrays.sort(catArray, new AnimalSizeComparator());
printDogs(catArray);
}
public static void printDogs(Animal[] animals){
for(Animal a: animals)
System.out.print("size="+a.size + " ");
System.out.println();
}
}
size=2 size=1 size=3
size=1 size=2 size=3
size=2 size=1 size=3
size=1 size=2 size=3
4.總結(jié)
1瘫辩、用通用父類。
2坛悉、策略模式伐厌。
3、合并排序-nlog(n)時(shí)間復(fù)雜度裸影。
4挣轨、Java.util.Collections.sort(List <T> list,Comparator <轩猩?super T> c)與Arrays.sort有類似的思想卷扮。