最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

1. 簡單示例

sort方法的使用非常的簡單明了,下面的例子中,先定義一個(gè)比較Dog大小的Comparator,然后將其實(shí)例對(duì)象作為參數(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)的一個(gè)完美又簡潔的示例,值得一提的是為什么這種場景下適合使用策略模式.

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

根據(jù)上面的例子,假設(shè)你想要根據(jù)Dog的重量來進(jìn)行排序,可以像下面這樣,創(chuàng)建一個(gè)新的比較器來進(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是一個(gè)接口,所以sort方法中可以傳入任意實(shí)現(xiàn)了此接口的類的實(shí)例,這就是策略模式的主要思想.

3.為何使用”super”

如果使用“Comparator<T>c”那是很簡單易懂的,但是sort的第2個(gè)參數(shù)里面的<?superT>意味著比較器所接受的類型可以是T或者它的超類.為什么是超類呢?答案是:這允許使用同一個(gè)比較器對(duì)不同的子類對(duì)象進(jìn)行比較.在下面的示例中很明顯地演示了這一點(diǎ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è)計(jì)模式(strategy pattern);
歸并排序(merge sort): 時(shí)間復(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)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他Java相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

  • Java中BigDecimal類的add()的使用詳解

    Java中BigDecimal類的add()的使用詳解

    這篇文章主要介紹了Java中BigDecimal類的add()的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 淺談Java中的class類

    淺談Java中的class類

    這篇文章主要介紹了淺談Java中的class類,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • java反射原理制作對(duì)象打印工具

    java反射原理制作對(duì)象打印工具

    本文主要給大家介紹了java反射原理制作對(duì)象打印工具的方法和代碼,以及一個(gè)熱心網(wǎng)友給出的更加簡潔方便的代碼,小伙伴們需要的話可以參考下。
    2015-12-12
  • 如何使用?Spring?Boot?搭建?WebSocket?服務(wù)器實(shí)現(xiàn)多客戶端連接

    如何使用?Spring?Boot?搭建?WebSocket?服務(wù)器實(shí)現(xiàn)多客戶端連接

    本文介紹如何使用SpringBoot快速搭建WebSocket服務(wù)器,實(shí)現(xiàn)多客戶端連接和消息廣播,WebSocket協(xié)議提供全雙工通信,SpringBoot通過@ServerEndpoint簡化配置,支持實(shí)時(shí)消息推送,適用于聊天室或通知系統(tǒng)等應(yīng)用場景
    2024-11-11
  • Java Web Fragment在項(xiàng)目中使用方法詳解

    Java Web Fragment在項(xiàng)目中使用方法詳解

    這篇文章主要介紹了Web Fragment在項(xiàng)目中使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • 聊一聊Java中的Steam流

    聊一聊Java中的Steam流

    當(dāng)我們需要處理的數(shù)據(jù)量很大的時(shí)候,為了提高性能,就需要使用到并行處理,這樣的處理方式是很復(fù)雜的,流可以幫助開發(fā)者節(jié)約寶貴的時(shí)間,讓以上的事情變得輕松,本文就和大家聊一聊Java中的Steam流,感興趣的同學(xué)跟著小編一起來看看吧
    2023-07-07
  • 通過實(shí)例解析Spring argNames屬性

    通過實(shí)例解析Spring argNames屬性

    這篇文章主要介紹了通過實(shí)例解析Spring argNames屬性,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • java中用數(shù)組實(shí)現(xiàn)環(huán)形隊(duì)列的示例代碼

    java中用數(shù)組實(shí)現(xiàn)環(huán)形隊(duì)列的示例代碼

    這篇文章主要介紹了java中用數(shù)組實(shí)現(xiàn)環(huán)形隊(duì)列的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • SpringBoot項(xiàng)目解決跨域的四種方案分享

    SpringBoot項(xiàng)目解決跨域的四種方案分享

    在用SpringBoot開發(fā)后端服務(wù)時(shí),我們一般是提供接口給前端使用,但前端通過瀏覽器調(diào)我們接口時(shí),瀏覽器會(huì)有個(gè)同源策略的限制,即協(xié)議,域名,端口任一不一樣時(shí)都會(huì)導(dǎo)致跨域,這篇文章主要介紹跨域的幾種常用解決方案,希望對(duì)大家有所幫助
    2023-05-05
  • Mybatis 插件原理解析

    Mybatis 插件原理解析

    mybatis是一款優(yōu)秀的ORM開源框架,這個(gè)框架具有極強(qiáng)的靈活性,本文再次給大家介紹Mybatis 插件原理,感興趣的朋友一起看看吧
    2021-10-10

最新評(píng)論

清远市| 托克托县| 荣昌县| 武夷山市| 商南县| 明光市| 白城市| 镇原县| 集贤县| 且末县| 隆安县| 图们市| 云浮市| 烟台市| 天全县| 利津县| 高陵县| 遂平县| 和田县| 毕节市| 呼伦贝尔市| 新民市| 岚皋县| 晋州市| 盐池县| 留坝县| 巩义市| 民乐县| 黄冈市| 延川县| 蒙自县| 中山市| 巩留县| 于田县| 邯郸市| 武义县| 犍为县| 潍坊市| 阿拉善盟| 闵行区| 五莲县|