Arrays.sort()你應該知道的事
本文由 ImportNew - 劉志軍 翻譯自 programcreek。如需轉載本文,請先參見文章末尾處的轉載要求。
ImportNew注:如果你也對Java技術翻譯分享感興趣,歡迎加入我們的 Java開發 小組。參與方式請查看小組簡介。
Arrays.sort(T[], Comparator < ? super T > c) 是用來對用戶自定義的對象數組排序功能的。Java 官方文檔簡單描述了它的作用,但不足以讓我們深刻理解。在這篇文章中,我將順着一下關鍵點息做個比較深入的理解。
1、簡單實例:如何使用Arrays.sort()
通過閱讀下面代碼,你能快速正確瞭解這個方法的用途。Comparator(比較器)用於根據Dogs的size比較其大小,並作爲sort方法的參數。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
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(); } } |
1
2
|
2
1
3 1
2
3 |
2、策略模式的使用
這是運用策略模式的一個很好的場景,爲什麼策略模式對於這種場景非常適用?簡單來說,策略模式使不同的算法在運行時得以選擇。在這個例子中,通過傳遞不同的Comparator,可以選擇不同的算法。基於上例,現在假設你有一個Comparator,用weight來代替size來比較Dogs。你可以簡單創建一個新的Comprator如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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(); } } |
輸出:
1
2
3
|
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僅僅是一個接口,任何實現了Comparator在運行時都可以被使用,這是策略模式的核心理念。
3、爲什麼使用“super”
很顯然,如果”Comparator<T>c”作爲參數,但是第二個參數是”Comparator< ? super T > c”,使用<? super T>意味着類型可以是T或者是它的超類。爲什麼允許超類型呢?答案是:這種方式允許所有子類使用同一個comparator。看看下面這個例子一目瞭然。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
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(); } } |
1
2
3
4
5
|
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、總結
總的來說,從Arrays.sort()中你應該瞭解到:
- generic(範型)——super
- 策略模式
- 歸併排序——nlog(n)時間複雜度
- java.util.Collections.sort(List<T>list, Comparator<?super T> c)類似於Arrays.sort