一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - java中的arrays.sort()代碼詳解

java中的arrays.sort()代碼詳解

2021-03-11 14:15Mrzhoug JAVA教程

這篇文章主要介紹了Java中的Arrays.sort()代碼詳解,涉及Arrays.sort()簡單示例,策略模式,”super”的使用等相關內容,具有一定借鑒價值,需要的朋友可以參考下。

Arrays.sort(T[], Comparator < ? super T > c) 方法用于對象數組按用戶自定義規則排序.
官方Java文檔只是簡要描述此方法的作用,并未進行詳細的介紹,本文將深入解析此方法。

1. 簡單示例

sort方法的使用非常的簡單明了,下面的例子中,先定義一個比較Dog大小的Comparator,然后將其實例對象作為參數傳給sort方法,通過此示例,你應該能夠快速掌握Arrays.sort()的使用方法。

java" id="highlighter_819099">
?
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
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.使用策略模式

這是策略模式(Strategypattern)的一個完美又簡潔的示例,值得一提的是為什么這種場景下適合使用策略模式.

總體來說,策略模式允許在程序執行時選擇不同的算法.比如在排序時,傳入不同的比較器(Comparator),就采用不同的算法.

根據上面的例子,假設你想要根據Dog的重量來進行排序,可以像下面這樣,創建一個新的比較器來進行排序:

?
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
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是一個接口,所以sort方法中可以傳入任意實現了此接口的類的實例,這就是策略模式的主要思想.

3.為何使用”super”

如果使用“Comparator<T>c”那是很簡單易懂的,但是sort的第2個參數里面的<?superT>意味著比較器所接受的類型可以是T或者它的超類.為什么是超類呢?答案是:這允許使用同一個比較器對不同的子類對象進行比較.在下面的示例中很明顯地演示了這一點:

?
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
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
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()相關的信息總結如下:

通用: super 類
策略設計模式(strategy pattern);
歸并排序(merge sort): 時間復雜度 n*log(n);
Java.util.Collections#sort(List < T > list, Comparator < ? super T > c)與Arrays.sort 使用類似的思想.

總結

以上就是本文關于Java中的Arrays.sort()代碼詳解的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他Java相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

原文鏈接:http://blog.csdn.net/Mrzhoug/article/details/51197626

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 热辣小秘书办公室 | 欧美肥胖bb | 四虎国产精品免费入口 | 日韩精品一区二区三区中文版 | 嗯啊好大视频 | 日韩二区三区 | 猫咪社区在线播放 | 极品ts赵恩静和直男激战啪啪 | 午夜dj影院在线视频观看完整 | 天天曰天天干 | 偷偷狠狠的日日高清完整视频 | 久久午夜夜伦痒痒想咳嗽P 久久无码AV亚洲精品色午夜麻豆 | 久9青青cao精品视频在线 | 国产婷婷综合丁香亚洲欧洲 | 叛佛 作者满栀小说免费阅读 | 500福利第一导航 | 国产三及| 午夜影院费试看黄 | 无人区尖叫之夜美女姐姐视频 | 秋霞黄色片 | 免费99精品国产自在现线 | www久久精品 | 亚洲免费视频在线观看 | 九九热视频 这里有精品 | 精品国产精品国产偷麻豆 | 青青青在线观看国产精品 | 亚洲国产中文字幕在线视频综合 | 美女全身无遮挡 | 欧美疯狂做爰3xxx | 欧美在线一级视频 | 日本不卡一区二区三区在线观看 | 青草欧美 | 香蕉久久夜色精品国产小优 | 亚洲不卡视频 | 免费观看二十女人一摸是水 | 亚洲AV久久无码精品九号 | 热久久最新视频 | 欧美又大又粗又爽视频 | 亚洲女人国产香蕉久久精品 | 奇米网狠狠网 | 久久中文字幕亚洲 |