A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Diferay 中级黑马   /  2014-2-23 20:45  /  1593 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Arrays.sort(T[], Comparator < ? super T > c) 是用来对用户自定义的对象数组排序功能的。Java 官方文档简单描述了它的作用,但不足以让我们深刻理解。为了更深入地理解它,这篇文章将梳理相关的关键点。

1、简单实例:如何使用Arrays.sort()

通过阅读下面代码,你能快速正确了解这个方法的用途。Comparator(比较器)用于根据Dogs的size比较其大小,并作为sort方法的参数。

  1. import java.util.Arrays;
  2. import java.util.Comparator;

  3. class Dog{
  4.     int size;   
  5.     public Dog(int s){
  6.         size = s;
  7.     }
  8. }

  9. class DogSizeComparator implements Comparator<Dog>{

  10.     @Override
  11.     public int compare(Dog o1, Dog o2) {
  12.         return o1.size - o2.size;
  13.     }
  14. }

  15. public class ArraySort {

  16.     public static void main(String[] args) {
  17.         Dog d1 = new Dog(2);
  18.         Dog d2 = new Dog(1);
  19.         Dog d3 = new Dog(3);

  20.         Dog[] dogArray = {d1, d2, d3};
  21.         printDogs(dogArray);

  22.         Arrays.sort(dogArray, new DogSizeComparator());
  23.         printDogs(dogArray);
  24.     }

  25.     public static void printDogs(Dog[] dogs){
  26.         for(Dog d: dogs)
  27.             System.out.print(d.size + " " );

  28.         System.out.println();
  29.     }
  30. }
复制代码

输出:

  1. 2 1 3
  2. 1 2 3
复制代码

2、策略模式的使用这是运用策略模式的一个很好的场景,为什么策略模式对于这种场景非常适用?简单来说,策略模式使不同的算法在运行时得以选择。在这个例子中,通过传递不同的Comparator,可以选择不同的算法。基于上例,现在假设你有一个Comparator,用weight来代替size来比较Dogs。你可以简单创建一个新的Comprator如下:
  1. class Dog{
  2.     int size;
  3.     int weight;

  4.     public Dog(int s, int w){
  5.         size = s;
  6.         weight = w;
  7.     }
  8. }

  9. class DogSizeComparator implements Comparator<Dog>{

  10.     @Override
  11.     public int compare(Dog o1, Dog o2) {
  12.         return o1.size - o2.size;
  13.     }
  14. }

  15. class DogWeightComparator implements Comparator<Dog>{

  16.     @Override
  17.     public int compare(Dog o1, Dog o2) {
  18.         return o1.weight - o2.weight;
  19.     }
  20. }

  21. public class ArraySort {

  22.     public static void main(String[] args) {
  23.         Dog d1 = new Dog(2, 50);
  24.         Dog d2 = new Dog(1, 30);
  25.         Dog d3 = new Dog(3, 40);

  26.         Dog[] dogArray = {d1, d2, d3};
  27.         printDogs(dogArray);

  28.         Arrays.sort(dogArray, new DogSizeComparator());
  29.         printDogs(dogArray);

  30.         Arrays.sort(dogArray, new DogWeightComparator());   
  31.         printDogs(dogArray);
  32.     }

  33.     public static void printDogs(Dog[] dogs){
  34.         for(Dog d: dogs)
  35.             System.out.print("size="+d.size + " weight=" + d.weight + " ");

  36.         System.out.println();
  37.     }
  38. }
复制代码

输出:
  1. size=2 weight=50 size=1 weight=30 size=3 weight=40
  2. size=1 weight=30 size=2 weight=50 size=3 weight=40
  3. size=1 weight=30 size=3 weight=40 size=2 weight=50
复制代码

Comparator仅仅是一个接口,任何实现了Comparator在运行时都可以被使用,这是策略模式的核心理念。3、为什么使用“super”很显然,如果”Comparator<T>c”作为参数,但是第二个参数是”Comparator< ? super T > c”,使用<? super T>意味着类型可以是T或者是它的超类。为什么允许超类型呢?答案是:这种方式允许所有子类使用同一个comparator。看看下面这个例子一目了然。
  1. import java.util.Arrays;
  2. import java.util.Comparator;

  3. class Animal{
  4.     int size;
  5. }

  6. class Dog extends Animal{
  7.     public Dog(int s){
  8.         size = s;
  9.     }
  10. }

  11. class Cat extends Animal{
  12.     public Cat(int s){
  13.         size  = s;
  14.     }
  15. }

  16. class AnimalSizeComparator implements Comparator<Animal>{

  17.     @Override
  18.     public int compare(Animal o1, Animal o2) {
  19.         return o1.size - o2.size;
  20.     }
  21.     //in this way, all sub classes of Animal can use this comparator.
  22. }

  23. public class ArraySort {

  24.     public static void main(String[] args) {
  25.         Dog d1 = new Dog(2);
  26.         Dog d2 = new Dog(1);
  27.         Dog d3 = new Dog(3);

  28.         Dog[] dogArray = {d1, d2, d3};
  29.         printDogs(dogArray);

  30.         Arrays.sort(dogArray, new AnimalSizeComparator());  
  31.         printDogs(dogArray);

  32.         System.out.println();

  33.         //when you have an array of Cat, same Comparator can be used.
  34.         Cat c1 = new Cat(2);
  35.         Cat c2 = new Cat(1);
  36.         Cat c3 = new Cat(3);

  37.         Cat[] catArray = {c1, c2, c3};
  38.         printDogs(catArray);

  39.         Arrays.sort(catArray, new AnimalSizeComparator());  
  40.         printDogs(catArray);
  41.     }

  42.     public static void printDogs(Animal[] animals){
  43.         for(Animal a: animals)
  44.             System.out.print("size="+a.size + " ");
  45.         System.out.println();
  46.     }
  47. }
复制代码

输出:
  1. size=2 size=1 size=3
  2. size=1 size=2 size=3

  3. size=2 size=1 size=3
  4. 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


评分

参与人数 1技术分 +1 收起 理由
朱神必 + 1

查看全部评分

5 个回复

倒序浏览
涨姿势了,谢谢分享
回复 使用道具 举报
学习了!
回复 使用道具 举报
刚学  有点看不懂 慢慢学吧  谢谢楼主分享
回复 使用道具 举报
谢谢分享
回复 使用道具 举报
感谢分享
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马