亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2017年12月25日 14:45:53   作者:Mrzhoug  
這篇文章主要介紹了Java中的Arrays.sort()代碼詳解,涉及Arrays.sort()簡單示例,策略模式,”super”的使用等相關(guān)內(nèi)容,具有一定借鑒價值,需要的朋友可以參考下。

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

1. 簡單示例

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

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();
	}
}

輸出為:

2 1 3 
1 2 3 

2.使用策略模式

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

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

根據(jù)上面的例子,假設(shè)你想要根據(jù)Dog的重量來進(jìn)行排序,可以像下面這樣,創(chuàng)建一個新的比較器來進(jìn)行排序:

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();
	}
}

執(zhí)行結(jié)果:

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方法中可以傳入任意實現(xiàn)了此接口的類的實例,這就是策略模式的主要思想.

3.為何使用”super”

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

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();
	}
}

輸出結(jié)果:

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

與Arrays.sort()相關(guān)的信息總結(jié)如下:

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

總結(jié)

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

相關(guān)文章

  • 如何用Stream解決兩層List屬性求和問題

    如何用Stream解決兩層List屬性求和問題

    這篇文章主要介紹了如何用Stream解決兩層List屬性求和問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • javaweb實現(xiàn)注冊登錄頁面

    javaweb實現(xiàn)注冊登錄頁面

    這篇文章主要為大家詳細(xì)介紹了javaweb實現(xiàn)注冊登錄頁面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 在Java中使用基本的正則表達(dá)式

    在Java中使用基本的正則表達(dá)式

    這篇文章主要介紹了在Java中使用基本的正則表達(dá)式,本文通過簡要的案例,說明了很多場景下的正則表達(dá)式的用法,列出了正則表達(dá)式匹配規(guī)則的表格,需要的朋友可以參考下
    2021-07-07
  • Feign遠(yuǎn)程調(diào)用參數(shù)里面內(nèi)容丟失的解決方案

    Feign遠(yuǎn)程調(diào)用參數(shù)里面內(nèi)容丟失的解決方案

    這篇文章主要介紹了Feign遠(yuǎn)程調(diào)用參數(shù)里面內(nèi)容丟失的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • SpringMVC開發(fā)restful API之用戶查詢代碼詳解

    SpringMVC開發(fā)restful API之用戶查詢代碼詳解

    這篇文章主要介紹了SpringMVC開發(fā)restful API之用戶查詢代碼詳解,小編覺得挺不錯的,這里分享給大家,需要的朋友可以參考。下面隨小編一起看看吧。
    2017-11-11
  • Java語言中的文件數(shù)據(jù)流示例詳解

    Java語言中的文件數(shù)據(jù)流示例詳解

    這篇文章主要為大家介紹了Java語言中的文件數(shù)據(jù)流示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Java圖像處理工具類

    Java圖像處理工具類

    這里給大家分享了一個java常用的圖像處理工具類,包含縮放圖像、切割圖像、圖像類型轉(zhuǎn)換、彩色轉(zhuǎn)黑白、文字水印、圖片水印等,有需要的小伙伴參考下。
    2015-02-02
  • Java編程實現(xiàn)軌跡壓縮之Douglas-Peucker算法詳細(xì)代碼

    Java編程實現(xiàn)軌跡壓縮之Douglas-Peucker算法詳細(xì)代碼

    這篇文章主要介紹了Java編程實現(xiàn)軌跡壓縮之Douglas-Peucker算法詳細(xì)代碼,具有一定借鑒價值,需要的朋友可以參考。
    2017-11-11
  • MybatisPlus逆向工程的項目實踐

    MybatisPlus逆向工程的項目實踐

    Mybatis-Plus逆向工程,是MP官方提供的一款代碼生成器,可以自動生成對應(yīng)的實體類、Mapper接口和配置文件,,本文主要介紹了MybatisPlus逆向工程的項目實踐,感興趣的可以了解一下
    2024-03-03
  • PageHelper插件實現(xiàn)一對多查詢時的分頁問題

    PageHelper插件實現(xiàn)一對多查詢時的分頁問題

    這篇文章主要介紹了PageHelper插件實現(xiàn)一對多查詢時的分頁問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評論