欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

專注于為中小企業(yè)提供網(wǎng)站制作、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)同安免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

1. 簡(jiǎn)單示例

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

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

根據(jù)上面的例子,假設(shè)你想要根據(jù)Dog的重量來(lái)進(jìn)行排序,可以像下面這樣,創(chuàng)建一個(gè)新的比較器來(lái)進(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”那是很簡(jiǎn)單易懂的,但是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()代碼詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他Java相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

網(wǎng)頁(yè)標(biāo)題:java中的arrays.sort()代碼詳解
文章鏈接:http://chinadenli.net/article20/gidhco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、定制開發(fā)做網(wǎng)站、標(biāo)簽優(yōu)化、響應(yīng)式網(wǎng)站、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司