亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

Arrays.sort() 你應(yīng)該知道的事

王偉廷 / 2227人閱讀

摘要:是用來(lái)對(duì)用戶自定義的對(duì)象數(shù)組排序功能的。官方文檔簡(jiǎn)單描述了它的作用,但不足以讓我們深刻理解。比較器用于根據(jù)的比較其大小,并作為方法的參數(shù)。輸出總結(jié)總的來(lái)說(shuō),從中你應(yīng)該了解到范型策略模式歸并排序時(shí)間復(fù)雜度類似于參考原文轉(zhuǎn)載自劉志軍

Arrays.sort(T[], Comparator < ? super T > c) 是用來(lái)對(duì)用戶自定義的對(duì)象數(shù)組排序功能的。Java 官方文檔簡(jiǎn)單描述了它的作用,但不足以讓我們深刻理解。為了更深入地理解它,這篇文章將梳理相關(guān)的關(guān)鍵點(diǎn)。

1、簡(jiǎn)單實(shí)例:如何使用Arrays.sort()

通過(guò)閱讀下面代碼,你能快速正確了解這個(gè)方法的用途。Comparator(比較器)用于根據(jù)Dogs的size比較其大小,并作為sort方法的參數(shù)。

import java.util.Arrays;
import java.util.Comparator;

class Dog{
    int size;   
    public Dog(int s){
        size = s;
    }
}

class DogSizeComparator implements Comparator{

    @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、策略模式的使用

這是運(yùn)用策略模式的一個(gè)很好的場(chǎng)景,為什么策略模式對(duì)于這種場(chǎng)景非常適用?簡(jiǎn)單來(lái)說(shuō),策略模式使不同的算法在運(yùn)行時(shí)得以選擇。在這個(gè)例子中,通過(guò)傳遞不同的Comparator,可以選擇不同的算法?;谏侠F(xiàn)在假設(shè)你有一個(gè)Comparator,用weight來(lái)代替size來(lái)比較Dogs。你可以簡(jiǎn)單創(chuàng)建一個(gè)新的Comprator如下:

class Dog{
    int size;
    int weight;

    public Dog(int s, int w){
        size = s;
        weight = w; 
    }
}

class DogSizeComparator implements Comparator{

    @Override
    public int compare(Dog o1, Dog o2) {
        return o1.size - o2.size;
    }
}

class DogWeightComparator implements Comparator{

    @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)了Comparator在運(yùn)行時(shí)都可以被使用,這是策略模式的核心理念。

3、為什么使用“super”

很顯然,如果”Comparatorc”作為參數(shù),但是第二個(gè)參數(shù)是”Comparator< ? super T > c”,使用意味著類型可以是T或者是它的超類。為什么允許超類型呢?答案是:這種方式允許所有子類使用同一個(gè)comparator??纯聪旅孢@個(gè)例子一目了然。

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{

    @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é)

總的來(lái)說(shuō),從Arrays.sort()中你應(yīng)該了解到:

generic(范型)——super

策略模式

歸并排序——nlog(n)時(shí)間復(fù)雜度

java.util.Collections.sort(Listlist, Comparator c)類似于Arrays.sort

參考:Arrays.sort(T[], java.util.Comparator)

原文:Deep Understanding of Arrays.sort()
轉(zhuǎn)載自:ImportNew.com - 劉志軍

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/69768.html

相關(guān)文章

  • java-工具類Collections和Arrays的設(shè)計(jì)和區(qū)別

    摘要:排序的算法是歸并排序。舉個(gè)例子,的算法可以不是使用歸并排序,但是該算法一定要是穩(wěn)定的。這個(gè)類是的一部分。官方這個(gè)類只包含操作或返回集合的靜態(tài)方法。具體來(lái)說(shuō)是,第一步,先把集合轉(zhuǎn)換為數(shù)組,第二步,調(diào)用。和沒有什么區(qū)別,只是傳參有點(diǎn)不同。 Arrays 1.作用看類的名字,就知道是對(duì)數(shù)組(數(shù)據(jù)類型[])進(jìn)行各種操作。例如,排序、查找、復(fù)制等。 排序的算法是歸并排序。查找的算法是二分查找。復(fù)...

    mj 評(píng)論0 收藏0
  • 剛寫完排序算法,就被開除了…

    摘要:剛寫完這段代碼,就被開除了棧長(zhǎng)前些天剛寫完上面這篇文章,沒幾天,又來(lái)一個(gè)悲劇。。。說(shuō)到這個(gè)程序員,讓我想起了最近審查代碼時(shí)候的幾個(gè)坑,真是讓人哭笑不得。。。示例直接不行寫這么繞,還把邏輯寫錯(cuò)了。 剛寫完這段代碼,就被開除了…… 棧長(zhǎng)前些天剛寫完上面這篇文章,沒幾天,又來(lái)一個(gè)悲劇。。。 據(jù)說(shuō)是一個(gè)月薪 9K 的 Java 程序員,因老板讓他寫一個(gè)排序算法,然后他就寫了一段屌炸天的休眠排序...

    klivitamJ 評(píng)論0 收藏0
  • Java Lambda表達(dá)式入門

    摘要:表達(dá)式還增強(qiáng)了集合庫(kù)。和前面的示例一樣先使用匿名內(nèi)部類來(lái)排序然后再使用表達(dá)式精簡(jiǎn)我們的代碼。使用舊的方式代碼如下所示使用匿名內(nèi)部類根據(jù)排序使用可以通過(guò)下面的代碼實(shí)現(xiàn)同樣的功能使用排序也可以采用如下形式其他的排序如下所示。 本文轉(zhuǎn)自:http://blog.csdn.net/renfufei...轉(zhuǎn)載請(qǐng)注明出處 原文鏈接: Start Using Java Lambda Expressi...

    youkede 評(píng)論0 收藏0
  • Java 性能調(diào)優(yōu)指南之 Java 集合概覽

    摘要:?jiǎn)尉€程集合本部分將重點(diǎn)介紹非線程安全集合。非線程安全集合框架的最新成員是自起推出的。這是標(biāo)準(zhǔn)的單線程陣營(yíng)中唯一的有序集合。該功能能有效防止運(yùn)行時(shí)造型。檢查個(gè)集合之間不存在共同的元素。基于自然排序或找出集合中的最大或最小元素。 【編者按】本文作者為擁有十年金融軟件開發(fā)經(jīng)驗(yàn)的 Mikhail Vorontsov,文章主要概覽了所有標(biāo)準(zhǔn) Java 集合類型。文章系國(guó)內(nèi) ITOM 管理平臺(tái) O...

    gnehc 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<