[转]Arrays.sort()你应该知道的事
以下内容转自:
原文链接: 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()中你应该了解到:
- generic(范型)——super
- 策略模式
- 归并排序——nlog(n)时间复杂度
- java.util.Collections.sort(List<T>list, Comparator<?super T> c)类似于Arrays.sort
[转]Arrays.sort()你应该知道的事的更多相关文章
- C# 范型约束 new() 你必须要知道的事
C# 范型约束 new() 你必须要知道的事 注意:本文不会讲范型如何使用,关于范型的概念和范型约束的使用请移步谷歌. 本文要讲的是关于范型约束无参构造函数 new 的一些底层细节和注意事项.写这篇文 ...
- 十件你需要知道的事,关于openstack-trove(翻译)
开源数据库即服务OpenStack Trove应该知道的10件事情 作者:Ken Rugg,Tesora首席执行官 Ken Rugg是Tesora的创始人,CEO和董事会成员. Ken的大部分职业都是 ...
- 关于Unicode,字符集,字符编码,每个程序员都应该知道的事
关于Unicode,字符集,字符编码,每个程序员都应该知道的事 作者:Jack47 李笑来的文章如何判断一个人是否聪明?中提到: 必要.清晰.且准确的概念,是一切思考的基石.所谓思考,很大程度上,就是 ...
- 学习IOS需要知道的事
什么是iOS iOS是一款由苹果公司开发的操作系统(OS是Operating System的简称),就像平时在电脑上用的Windows XP.Windows 7,都是操作系统 那什么是操作系统呢?操作 ...
- 网站开发进阶(三十八)Web前端开发规范文档你需要知道的事
Web前端开发规范文档你需要知道的事 规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档. 本规范文档一经确认, 前端开发人员必须按本文档规范进 ...
- Web前端开发规范文档你需要知道的事
Web前端开发规范文档你需要知道的事 规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档. 本规范文档一经确认, 前端开发人员必须按本文档规范进 ...
- 漫谈ElasticSearch关于ES性能调优几件必须知道的事
lasticSearch是现在技术前沿的大数据引擎,常见的组合有ES+Logstash+Kibana作为一套成熟的日志系统,其中Logstash是ETL工具,Kibana是数据分析展示平台.ES让人惊 ...
- Weex学习与实践(一):Weex,你需要知道的事
Weex学习与实践(一):Weex,你需要知道的事 http://coderyi.com/posts/weex1/ 1.命令行工具:weex-toolkit https://github.com/w ...
- 苹果强制使用HTTPS传输了怎么办?——关于HTTPS,APP开发者必须知道的事
WeTest 导读 2017年1月1日起,苹果公司将强制使用HTTPS协议传输.本文通过对HTTPS基础原理和通信过程内容的讲解,介绍APP开发者在这个背景下的应对办法. 几周前,我们在<htt ...
随机推荐
- 《Linux内核设计的艺术》学习笔记(五)INT 0x10中断
参考书籍: 1. <IBM-PC汇编语言程序设计> 2. http://www.ctyme.com/intr/int-10.htm ◆ 设置显示方式: 功能号:AH = 00H 调用参 ...
- mysql概要(二)类型
1.mysql数值型范围 tinyint可选属性 tinyint(N) unsigned zerofill N:表示显示长度,与zerofill配合使用,即长度不够用0填充,并且自动变成无符号的数,仅 ...
- springmvc:frameServletBean
1.httpservlerBean 主要初始化web.xml的init-param参数 2.frameWorkServlet 该类作用 1.applicationConetxt用于配置整体 webap ...
- springmvc企业级开发实战
一.用eclipse插件搭建项目 二.pom文件 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...
- [转载] Linux进程关系
在工作中, 主进程创建了子进程, 而子进程又创建了孙子进程, 然而子进程被莫名其妙的 kill 了, 结果主进程又启动了一个子进程, 子进程又尝试创建孙子进程, 但是这时候就有问题了, 因为孙子进程还 ...
- strcpy, memcpy, memset函数
一. strcpy函数 原型声明:char *strcpy(char* dest, const char *src); 头文件:#include <string.h> 和 #inclu ...
- Android dex分包方案
当一个app的功能越来越复杂,代码量越来越多,也许有一天便会突然遇到下列现象: 1. 生成的apk在2.3以前的机器无法安装,提示INSTALL_FAILED_DEXOPT 2. 方法数量过多,编译时 ...
- 在Eclipse中设置Java类上面的注释(包含作者、日期等)
希望在Eclipse中,让Java类上面的注释像下面这样,改如何设置呢? 在Eclipse中,点击菜单中的Window-->Preferences,打开如下窗口:
- foundation系列
1如何将布尔值转为OC对象? 1把 BOOL 值包装到 NSNumber中: NSNumber *boolNumber = [NSNumber numberWithBool:YES] 2获取BOO ...
- 使用TCP/IP的套接字(Socket)进行通信
http://www.cnblogs.com/mengdd/archive/2013/03/10/2952616.html 使用TCP/IP的套接字(Socket)进行通信 套接字Socket的引入 ...