Arrays.sort()你应该知道的事

来源:互联网 发布:网络中的涵化理论 编辑:程序博客网 时间:2024/06/08 18:35

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
importjava.util.Arrays;
importjava.util.Comparator;
 
classDog{
    intsize;  
    publicDog(ints){
        size = s;
    }
}
 
classDogSizeComparator implementsComparator<Dog>{
 
    @Override
    publicint compare(Dog o1, Dog o2) {
        returno1.size - o2.size;
    }
}
 
publicclass ArraySort {
 
    publicstatic void main(String[] args) {
        Dog d1 = newDog(2);
        Dog d2 = newDog(1);
        Dog d3 = newDog(3);
 
        Dog[] dogArray = {d1, d2, d3};
        printDogs(dogArray);
 
        Arrays.sort(dogArray,newDogSizeComparator());
        printDogs(dogArray);
    }
 
    publicstatic void printDogs(Dog[] dogs){
        for(Dog d: dogs)
            System.out.print(d.size + " " );
 
        System.out.println();
    }
}
输出:
1
2
21 3
12 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
classDog{
    intsize;
    intweight;
 
    publicDog(ints, intw){
        size = s;
        weight = w;
    }
}
 
classDogSizeComparator implementsComparator<Dog>{
 
    @Override
    publicint compare(Dog o1, Dog o2) {
        returno1.size - o2.size;
    }
}
 
classDogWeightComparator implementsComparator<Dog>{
 
    @Override
    publicint compare(Dog o1, Dog o2) {
        returno1.weight - o2.weight;
    }
}
 
publicclass ArraySort {
 
    publicstatic void main(String[] args) {
        Dog d1 = newDog(2,50);
        Dog d2 = newDog(1,30);
        Dog d3 = newDog(3,40);
 
        Dog[] dogArray = {d1, d2, d3};
        printDogs(dogArray);
 
        Arrays.sort(dogArray,newDogSizeComparator());
        printDogs(dogArray);
 
        Arrays.sort(dogArray,newDogWeightComparator());  
        printDogs(dogArray);
    }
 
    publicstatic void printDogs(Dog[] dogs){
        for(Dog d: dogs)
            System.out.print("size="+d.size + " weight=" + d.weight + " ");
 
        System.out.println();
    }
}

输出:

1
2
3
size=2weight=50size=1weight=30size=3weight=40
size=1weight=30size=2weight=50size=3weight=40
size=1weight=30size=3weight=40size=2weight=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
importjava.util.Arrays;
importjava.util.Comparator;
 
classAnimal{
    intsize;
}
 
classDog extendsAnimal{
    publicDog(ints){
        size = s;
    }
}
 
classCat extendsAnimal{
    publicCat(ints){
        size  = s;
    }
}
 
classAnimalSizeComparator implementsComparator<Animal>{
 
    @Override
    publicint compare(Animal o1, Animal o2) {
        returno1.size - o2.size;
    }
    //in this way, all sub classes of Animal can use this comparator.
}
 
publicclass ArraySort {
 
    publicstatic void main(String[] args) {
        Dog d1 = newDog(2);
        Dog d2 = newDog(1);
        Dog d3 = newDog(3);
 
        Dog[] dogArray = {d1, d2, d3};
        printDogs(dogArray);
 
        Arrays.sort(dogArray,newAnimalSizeComparator()); 
        printDogs(dogArray);
 
        System.out.println();
 
        //when you have an array of Cat, same Comparator can be used.
        Cat c1 = newCat(2);
        Cat c2 = newCat(1);
        Cat c3 = newCat(3);
 
        Cat[] catArray = {c1, c2, c3};
        printDogs(catArray);
 
        Arrays.sort(catArray,newAnimalSizeComparator()); 
        printDogs(catArray);
    }
 
    publicstatic void printDogs(Animal[] animals){
        for(Animal a: animals)
            System.out.print("size="+a.size + " ");
        System.out.println();
    }
}
输出:
1
2
3
4
5
size=2size=1size=3
size=1size=2size=3
 
size=2size=1size=3
size=1size=2size=3

4、总结

总的来说,从Arrays.sort()中你应该了解到:

  1. generic(范型)——super
  2. 策略模式
  3. 归并排序——nlog(n)时间复杂度
  4.  java.util.Collections.sort(List<T>list, Comparator<?super T> c)类似于Arrays.sort
0 0
原创粉丝点击