以下内容转自:

  原文链接: programcreek 翻译: ImportNew.com刘志军
  译文链接: http://www.importnew.com/8952.html

-------------------------------------------------------

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<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、策略模式的使用

这是运用策略模式的一个很好的场景,为什么策略模式对于这种场景非常适用?简单来说,策略模式使不同的算法在运行时得以选择。在这个例子中,通过传递不同的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<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

3、为什么使用“super”

很显然,如果”Comparator<T>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<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();
}
}

输出:

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、总结

总的来说,从Arrays.sort()中你应该了解到:

  1. generic(范型)——super
  2. 策略模式
  3. 归并排序——nlog(n)时间复杂度
  4. java.util.Collections.sort(List<T>list, Comparator<?super T> c)类似于Arrays.sort

[转]Arrays.sort()你应该知道的事的更多相关文章

  1. C# 范型约束 new() 你必须要知道的事

    C# 范型约束 new() 你必须要知道的事 注意:本文不会讲范型如何使用,关于范型的概念和范型约束的使用请移步谷歌. 本文要讲的是关于范型约束无参构造函数 new 的一些底层细节和注意事项.写这篇文 ...

  2. 十件你需要知道的事,关于openstack-trove(翻译)

    开源数据库即服务OpenStack Trove应该知道的10件事情 作者:Ken Rugg,Tesora首席执行官 Ken Rugg是Tesora的创始人,CEO和董事会成员. Ken的大部分职业都是 ...

  3. 关于Unicode,字符集,字符编码,每个程序员都应该知道的事

    关于Unicode,字符集,字符编码,每个程序员都应该知道的事 作者:Jack47 李笑来的文章如何判断一个人是否聪明?中提到: 必要.清晰.且准确的概念,是一切思考的基石.所谓思考,很大程度上,就是 ...

  4. 学习IOS需要知道的事

    什么是iOS iOS是一款由苹果公司开发的操作系统(OS是Operating System的简称),就像平时在电脑上用的Windows XP.Windows 7,都是操作系统 那什么是操作系统呢?操作 ...

  5. 网站开发进阶(三十八)Web前端开发规范文档你需要知道的事

    Web前端开发规范文档你需要知道的事 规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档. 本规范文档一经确认, 前端开发人员必须按本文档规范进 ...

  6. Web前端开发规范文档你需要知道的事

    Web前端开发规范文档你需要知道的事 规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档. 本规范文档一经确认, 前端开发人员必须按本文档规范进 ...

  7. 漫谈ElasticSearch关于ES性能调优几件必须知道的事

    lasticSearch是现在技术前沿的大数据引擎,常见的组合有ES+Logstash+Kibana作为一套成熟的日志系统,其中Logstash是ETL工具,Kibana是数据分析展示平台.ES让人惊 ...

  8. Weex学习与实践(一):Weex,你需要知道的事

    Weex学习与实践(一):Weex,你需要知道的事 http://coderyi.com/posts/weex1/ 1.命令行工具:weex-toolkit  https://github.com/w ...

  9. 苹果强制使用HTTPS传输了怎么办?——关于HTTPS,APP开发者必须知道的事

    WeTest 导读 2017年1月1日起,苹果公司将强制使用HTTPS协议传输.本文通过对HTTPS基础原理和通信过程内容的讲解,介绍APP开发者在这个背景下的应对办法. 几周前,我们在<htt ...

随机推荐

  1. POJ 2431 Expedition(探险)

    POJ 2431 Expedition(探险) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] A group of co ...

  2. C# 线程(一)

    From : http://www.cnblogs.com/miniwiki/archive/2010/06/18/1760540.html 文章系参考转载,英文原文网址请参考:http://www. ...

  3. SQL Server索引 (原理、存储)聚集索引、非聚集索引、堆 <第一篇>

    一.存储结构 在SQL Server中,有许多不同的可用排列规则选项. 二进制:按字符的数字表示形式排序(ASCII码中,用数字32表示空格,用68表示字母"D").因为所有内容都 ...

  4. Spring依赖注入

    依赖注入: 使用构造器注入 使用属性setter方法注入 使用Field注入(用于注解方式) 注入依赖对象可以采用手工装配或自动装配,在实际应用中建议使用手工装配,因为自动装配会产生未知情况,开发人员 ...

  5. 慎重别选择到"僵尸"软件

    何谓僵尸? 没有灵魂,动作单一,我们电视电影上经常看见, 只能往前跳,不会走路, 手向前伸直,左右摆攻击. 何谓"僵尸"软件? 根据僵尸的特性,大概有如下几类: 1.没有任何创新性 ...

  6. iOS - OC 术语表

    1.术语表

  7. accessor method & mutator method

    import java.time.*; public class MyTest{ public static void main(String[] args){ LocalDate date = Lo ...

  8. 定义函数def

  9. android 自定义进度条颜色

    android 自定义进度条颜色 先看图 基于产品经理各种自定义需求,经过查阅了解,下面是自己对Android自定义进度条的学习过程!   这个没法了只能看源码了,还好下载了源码, sources\b ...

  10. java集合和scala集合互转

    使用 scala.collection.JavaConverters 与Java集合交互.它有一系列的隐式转换,添加了asJava和asScala的转换方法.使用它们这些方法确保转换是显式的,有助于阅 ...