专栏名称: ImportNew
伯乐在线旗下账号,专注Java技术分享,包括Java基础技术、进阶技能、架构设计和Java技术领域动态等。
目录
相关文章推荐
芋道源码  ·  Nginx性能优化的几个方法 ·  4 小时前  
芋道源码  ·  还在用Jenkins?快来试试这款简而轻的自 ... ·  4 小时前  
芋道源码  ·  ES+MySQL优雅的实现模糊搜索 ·  昨天  
Java编程精选  ·  巧用 SpringEvent 解决 ... ·  3 天前  
芋道源码  ·  目前对Grok 3分析最为透彻的一篇文章 ·  2 天前  
51好读  ›  专栏  ›  ImportNew

Arrays.sort() 你应该知道的事

ImportNew  · 公众号  · Java  · 2017-05-11 11:39

正文

(点击 上方蓝字 ,快速关注我们)


编译:ImportNew - 刘志军

如有好文章投稿,请点击 → 这里了解详情


Arrays.sort(T[], Comparator < ? super T > c) 是用来对用户自定义的对象数组排序功能的。Java 官方文档简单描述了它的作用,但不足以让我们深刻理解。为了更深入地理解它,这篇文章将梳理相关的关键点。


1、简单实例:如何使用Arrays.sort()


通过阅读下面代码,你能快速正确了解这个方法的用途。Comparator(比较器)用于根据Dogs的size比较其大小,并作为sort方法的参数。


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 3


2、策略模式的使用


这是运用策略模式的一个很好的场景,为什么策略模式对于这种场景非常适用?简单来说,策略模式使不同的算法在运行时得以选择。在这个例子中,通过传递不同的Comparator,可以选择不同的算法。基于上例,现在假设你有一个Comparator,用weight来代替size来比较Dogs。你可以简单创建一个新的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仅仅是一个接口,任何实现了Comparator在运行时都可以被使用,这是策略模式的核心理念。


3、为什么使用“super”


很显然,如果”Comparator c”作为参数,但是第二个参数是”Comparator< ? super T > c”,使用 super T>意味着类型可以是T或者是它的超类。为什么允许超类型呢?答案是:这种方式允许所有子类使用同一个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);







请到「今天看啥」查看全文