摘要:是用來(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 32、策略模式的使用
這是運(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”很顯然,如果”Comparator
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=34、總結(jié)
總的來(lái)說(shuō),從Arrays.sort()中你應(yīng)該了解到:
generic(范型)——super
策略模式
歸并排序——nlog(n)時(shí)間復(fù)雜度
java.util.Collections.sort(List
參考: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
摘要:排序的算法是歸并排序。舉個(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ù)...
摘要:剛寫完這段代碼,就被開除了棧長(zhǎng)前些天剛寫完上面這篇文章,沒幾天,又來(lái)一個(gè)悲劇。。。說(shuō)到這個(gè)程序員,讓我想起了最近審查代碼時(shí)候的幾個(gè)坑,真是讓人哭笑不得。。。示例直接不行寫這么繞,還把邏輯寫錯(cuò)了。 剛寫完這段代碼,就被開除了…… 棧長(zhǎng)前些天剛寫完上面這篇文章,沒幾天,又來(lái)一個(gè)悲劇。。。 據(jù)說(shuō)是一個(gè)月薪 9K 的 Java 程序員,因老板讓他寫一個(gè)排序算法,然后他就寫了一段屌炸天的休眠排序...
摘要:表達(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...
摘要:?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...
閱讀 1234·2021-11-23 10:04
閱讀 2477·2021-11-22 15:29
閱讀 3140·2021-11-19 09:40
閱讀 791·2021-09-22 15:26
閱讀 2192·2019-08-29 16:27
閱讀 2563·2019-08-29 16:10
閱讀 1979·2019-08-29 15:43
閱讀 3386·2019-08-29 12:43