[转]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内核设计的艺术》学习笔记(六)执行setup.s
参考资料 1. 8259A可编程中断控制器 jmpi , SETUPSEG // 0x90200 到这里,bootsect.s的执行就算结束了.控制权转移到了setup.s文件的手中. setup程序 ...
- 2013 Multi-University Training Contest 6
HDU-4655 Cut Pieces 题意:有N个格子能够被涂色,每个格子能够涂1-ai 种颜色,当N=6,涂色方案:112233 认为方案中共有3个颜色块:涂色方案:121212 认为方案中共有6 ...
- iOS - UIActivityViewController
前言 NS_CLASS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED @interface UIActivityViewController : UIViewControl ...
- iOS - UIWebView
前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIWebView : UIView <NSCoding, UIScrol ...
- VideoView
[1]这个控件就是对surfaceview 和 meidiaplayer进行封装 [2]meidiaplayer 播放视频他只支持 3gp MP4格式
- linux登录mysql
mysql -u 用户名 -p密码 mysql -u root -psqj888
- spring data jpa 创建方法名进行简单查询
版权声明:本文为博主原创文章,未经博主允许不得转载. spring data jpa 可以通过在接口中按照规定语法创建一个方法进行查询,spring data jpa 基础接口中,如CrudRepos ...
- 转!!URL和URI区别
URI,URL,URN 从上面的那幅图可以看出来,一共有三个不同的概念URI,URL,URN.这讨论这样的问题时,最好的方法就是回到原点啊,这里我们在RFC 3986: Uniform Resourc ...
- jquery中DOM
节点包裹 wrap() (1)$().wrap(html) 将选择的节点用指定的元素包装 $('p').wrap('<div></div>'); (2)多层包裹 $('p'). ...
- SVN使用汇总
SVN项目管理文件夹:Tag/Branch/Trunk Trunk:在我经历的开发中,新建Trunk意味着对旧Trunk的一个保留,同时在新的Trunk中可以进行新功能的开发及对已有功能进行完善: B ...