资讯专栏INFORMATION COLUMN

Arrays.sort() 你应该知道的事

王伟廷 / 1992人阅读

摘要:是用来对用户自定义的对象数组排序功能的。官方文档简单描述了它的作用,但不足以让我们深刻理解。比较器用于根据的比较其大小,并作为方法的参数。输出总结总的来说,从中你应该了解到范型策略模式归并排序时间复杂度类似于参考原文转载自刘志军

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{

    @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{

    @Override
    public int compare(Dog o1, Dog o2) {
        return o1.size - o2.size;
    }
}

class DogWeightComparator implements Comparator{

    @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仅仅是一个接口,任何实现了Comparator在运行时都可以被使用,这是策略模式的核心理念。

3、为什么使用“super”

很显然,如果”Comparatorc”作为参数,但是第二个参数是”Comparator< ? super T > c”,使用意味着类型可以是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{

    @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(Listlist, Comparator c)类似于Arrays.sort

参考:Arrays.sort(T[], java.util.Comparator)

原文:Deep Understanding of Arrays.sort()
转载自:ImportNew.com - 刘志军

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/69768.html

相关文章

  • java-工具类Collections和Arrays的设计和区别

    摘要:排序的算法是归并排序。举个例子,的算法可以不是使用归并排序,但是该算法一定要是稳定的。这个类是的一部分。官方这个类只包含操作或返回集合的静态方法。具体来说是,第一步,先把集合转换为数组,第二步,调用。和没有什么区别,只是传参有点不同。 Arrays 1.作用看类的名字,就知道是对数组(数据类型[])进行各种操作。例如,排序、查找、复制等。 排序的算法是归并排序。查找的算法是二分查找。复...

    mj 评论0 收藏0
  • 刚写完排序算法,就被开除了…

    摘要:刚写完这段代码,就被开除了栈长前些天刚写完上面这篇文章,没几天,又来一个悲剧。。。说到这个程序员,让我想起了最近审查代码时候的几个坑,真是让人哭笑不得。。。示例直接不行写这么绕,还把逻辑写错了。 刚写完这段代码,就被开除了…… 栈长前些天刚写完上面这篇文章,没几天,又来一个悲剧。。。 据说是一个月薪 9K 的 Java 程序员,因老板让他写一个排序算法,然后他就写了一段屌炸天的休眠排序...

    klivitamJ 评论0 收藏0
  • Java Lambda表达式入门

    摘要:表达式还增强了集合库。和前面的示例一样先使用匿名内部类来排序然后再使用表达式精简我们的代码。使用旧的方式代码如下所示使用匿名内部类根据排序使用可以通过下面的代码实现同样的功能使用排序也可以采用如下形式其他的排序如下所示。 本文转自:http://blog.csdn.net/renfufei...转载请注明出处 原文链接: Start Using Java Lambda Expressi...

    youkede 评论0 收藏0
  • Java 性能调优指南之 Java 集合概览

    摘要:单线程集合本部分将重点介绍非线程安全集合。非线程安全集合框架的最新成员是自起推出的。这是标准的单线程阵营中唯一的有序集合。该功能能有效防止运行时造型。检查个集合之间不存在共同的元素。基于自然排序或找出集合中的最大或最小元素。 【编者按】本文作者为拥有十年金融软件开发经验的 Mikhail Vorontsov,文章主要概览了所有标准 Java 集合类型。文章系国内 ITOM 管理平台 O...

    gnehc 评论0 收藏0

发表评论

0条评论

王伟廷

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<