深入理解Arrays.sort()
Arrays.sort(T[], Comparator < ? super T > c) 方法用于对象数组按用户自定义规则排序.
官方Java文档只是简要描述此方法的作用,并未进行详细的介绍,本文将深入解析此方法。
1. 简单示例
sort方法的使用非常的简单明了,下面的例子中,先定义一个比较Dog大小的Comparator,然后将其实例对象作为参数传给sort方法,通过此示例,你应该能够快速掌握Arrays.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. 使用策略模式
这是策略模式( Strategy pattern )的一个完美又简洁的示例,值得一提的是为什么这种场景下适合使用策略模式.
总体来说,策略模式允许在程序执行时选择不同的算法.比如在排序时,传入不同的比较器(Comparator),就采用不同的算法.
根据上面的例子,假设你想要根据Dog的重量来进行排序,可以像下面这样,创建一个新的比较器来进行排序:
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
Comparator 是一个接口,所以sort方法中可以传入任意实现了此接口的类的实例,这就是策略模式的主要思想.
3. 为何使用"super"
如果使用 "Comparator < T > c" 那是很简单易懂的,但是sort的第2个参数里面的 < ? super T > 意味着比较器所接受的类型可以是T或者它的超类. 为什么是超类呢? 答案是: 这允许使用同一个比较器对不同的子类对象进行比较.在下面的示例中很明显地演示了这一点:
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()相关的信息总结如下:
- 通用: super 类
- 策略设计模式(strategy pattern);
- 归并排序(merge sort): 时间复杂度 n*log(n);
- Java.util.Collections#sort(List < T > list, Comparator < ? super T > c)与Arrays.sort 使用类似的思想.
深入理解Arrays.sort()相关推荐
- Java中关于Arrays.sort的两种重载方法的理解
前言 在java中重载排序方法的方法目前有两种,一种是实现Comparable接口的compareTo方法,还有一种是用比较器(comparator) 作为参数,其中比较器是实现了Comparator ...
- 深入理解 Comparable 接口和 Comparator 接口以及Arrays.sort()
compareTo(Object o)方法是java.lang.Comparable<T>接口中的方法,当需要对某个类的对象进行排序时,该类需要实现Comparable<T>接 ...
- 对于二维数组,如何用Arrays.sort()进行排序以及理解------通俗易懂,条理清晰
public static void main(String[] args) {int[][] nums = {{5, 0}, {4, 1}, {6, 2}};//重写Comparator接口里面的c ...
- java arrays.sort() c_5.4 (Java学习笔记)集合的排序(Collections.sort(),及Arrays.sort())...
1.Comparable接口 这个接口顾名思义就是用于排序的,如果要对某些对象进行排序,那么该对象所在的类必须实现 Comparabld接口.Comparable接口只有一个方法CompareTo() ...
- Arrays.sort() 为什么可以对 int 等数组进行排序?我跟面试官扯了半个小时 | 原力计划...
作者 | TrueDei 责编 | 王晓曼 出品 | CSDN博客 前言 排序是在程序开发中最常用到的,最常见的就是针对一些数字进行排序.而现实中像商品的名字,订单的日期等进行排序.Java的JDK中 ...
- java list逆序_Java的数组和list升序,降序,逆序函数Collections.sort和Arrays.sort的使用...
list升序,降序,逆序 Listlist =new ArrayList(); //如果list是 5 7 2 6 8 1 4 1.升序: Collections.sort(list) //list: ...
- Arrays.sort() and 吾欲仁则斯仁至矣
有不少人在争论 Java 对学生有益还是有害(不少网站上的评论有断章取义的嫌疑,不值得多看,建议看这个中文链接)(英文链接).事实上,这个问题事实上不是最近才提出来,大约6~7 年前,我们的同事在美国 ...
- Arrays.sort重写排序顺序
(2条消息) java comparator 升序.降序.倒序从源码角度理解_山鬼谣me的博客-CSDN博客_comparator 倒序 //必须同时写这三个if条件来return 1.-1.0//升 ...
- Java数组排序: Array-ArrayList-List-Collections.sort()/List.sort()/Arrays.sort()
文章目录 ArrayList/List 的排序:Collections.sort()/List.sort() Array 的排序:Arrays.sort() 此文首发于我的Jekyll博客:zhang ...
最新文章
- bootstrap17-响应式表格布局
- 程序员面试题100题第29题——调整数组顺序使奇数位于偶数前面
- Intel VT学习笔记(四)—— VMCS(下)
- C++ class实现单向循环链表(完整代码)
- Grunt上手指南(转)
- 如何将XML文件导入Excel中
- flask blueprint
- 7个使用JavaScript构建数据可视化的实用工具库,希望你能喜欢
- k8s高可用集群_搭建高可用集群(初始化和部署keepalived)---K8S_Google工作笔记0055
- win10 mysql数据库中文乱码
- SLAM学习笔记-------------(10)后端2
- nodejs+vue+elementui宠物医院管理系统php-java-python
- 机动车车牌分类及命名规则总结
- html 屏蔽广告显示,JS判断网页广告被屏蔽解,广告终结者、AdGuard插件判断并提示...
- 贴片钽电容封封装及规格和参数资料
- python画满天星_跟我学解Python题-海龟制图满天星
- 一个懒得程序员才是一个好的程序员
- 全球顶级的14位程序员!
- ES6(ES2015)
- 快速集成腾讯云短信实现短信发送(Spring Boot)