以下内容转自:

  原文链接: 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. python_way day16 DOM

    Python_way day16 1.Dom  (找到html中的标签) 一.DOM 1.查找元素 直接查找 document.getElementById 根据ID获取一个标签 --->这里是 ...

  2. MVC 3 数据验证 Model Validation 详解

    在MVC 3中 数据验证,已经应用的非常普遍,我们在web form时代需要在View端通过js来验证每个需要验证的控件值,并且这种验证的可用性很低.但是来到了MVC 新时代,我们可以通过MVC提供的 ...

  3. updatePanel导致JS失效的解决办法(转)

    吐槽下,维护别人之前做的项目好蛋疼,整个页面都是用微软的ajax框架. 今天给repeater用JS写一个hover事件 <script type="text/javascript&q ...

  4. 新知识Tom大叔

    http://www.cnblogs.com/TomXu/archive/2011/12/15/2284752.html http://www.cnblogs.com/TomXu/archive/20 ...

  5. [转载] linux cgroup

    原文: http://coolshell.cn/articles/17049.html 感谢左耳朵耗子的精彩文章. 前面,我们介绍了Linux Namespace,但是Namespace解决的问题主要 ...

  6. Swift语言学习之OC和Swift混编

    本文转自http://www.helloswift.com.cn/swiftbase/2015/0112/3469.html iOS OC和Swift混编 1.创建一个swift或者oc的工程:我这里 ...

  7. INTERPRETER(解释器)

    1 意图:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子. 2 动机:如果一种特定类型的问题发生的频率足够高,那么可能就值得将该问题的各个实例表述为一个 ...

  8. drush cc all 报错

    请看好 指明了Module文件的行数 报错一定要多看看哦.

  9. Libsvm:脚本(subset.py、grid.py、checkdata.py) | MATLAB/OCTAVE interface | Python interface

    1.脚本 This directory includes some useful codes: 1. subset selection tools. (子集抽取工具) subset.py 2. par ...

  10. const修饰指针

    关于const修饰指针的情况,一般分为如下4种情况: ; const int *a =&b; //情况1 int const *a =&b; //情况2 int* const a =& ...