Arrays.sort(T[], Comparator < ? super T > c)
是用来对用户自定义的对象数组排序功能的。Java 官方文档简单描述了它的做用,但不足以让咱们深入理解。为了更深刻地理解它,这篇文章将梳理相关的关键点。html
经过阅读下面代码,你能快速正确了解这个方法的用途。Comparator(比较器)用于根据Dogs的size比较其大小,并做为sort方法的参数。java
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
这是运用策略模式的一个很好的场景,为何策略模式对于这种场景很是适用?简单来讲,策略模式使不一样的算法在运行时得以选择。在这个例子中,经过传递不一样的Comparator,能够选择不一样的算法。基于上例,如今假设你有一个Comparator,用weight来代替size来比较Dogs。你能够简单建立一个新的Comprator以下:api
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(); } }
输出:数组
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仅仅是一个接口,任何实现了Comparator在运行时均可以被使用,这是策略模式的核心理念。oracle
很显然,若是”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<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(); } }
输出:this
size=2 size=1 size=3 size=1 size=2 size=3 size=2 size=1 size=3 size=1 size=2 size=3
总的来讲,从Arrays.sort()中你应该了解到:code
参考:Arrays.sort(T[], java.util.Comparator)htm
原文:Deep Understanding of Arrays.sort()
转载自:ImportNew.com - 刘志军