zoukankan      html  css  js  c++  java
  • 关于Arrays类总结

    Arrays是java中的工具类,其中所有的方法都是static.类名就可以直接调用其中的方法.

    本文部分引用自:

    http://www.importnew.com/8952.html

    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  
     4 class Dog{
     5     int size;   
     6     public Dog(int s){
     7         size = s;
     8     }
     9 }
    10  
    11 class DogSizeComparator implements Comparator<Dog>{
    12  
    13     @Override
    14     public int compare(Dog o1, Dog o2) {
    15         return o1.size - o2.size;
    16     }
    17 }
    18  
    19 public class ArraySort {
    20  
    21     public static void main(String[] args) {
    22         Dog d1 = new Dog(2);
    23         Dog d2 = new Dog(1);
    24         Dog d3 = new Dog(3);
    25  
    26         Dog[] dogArray = {d1, d2, d3};
    27         printDogs(dogArray);
    28  
    29         Arrays.sort(dogArray, new DogSizeComparator()); 
    30         printDogs(dogArray);
    31     }
    32  
    33     public static void printDogs(Dog[] dogs){
    34         for(Dog d: dogs)
    35             System.out.print(d.size + " " );
    36  
    37         System.out.println();
    38     }
    39 }

    输出:

    2 1 3
    1 2 3

    2、策略模式的使用

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

     1 class Dog{
     2     int size;
     3     int weight;
     4  
     5     public Dog(int s, int w){
     6         size = s;
     7         weight = w; 
     8     }
     9 }
    10  
    11 class DogSizeComparator implements Comparator<Dog>{
    12  
    13     @Override
    14     public int compare(Dog o1, Dog o2) {
    15         return o1.size - o2.size;
    16     }
    17 }
    18  
    19 class DogWeightComparator implements Comparator<Dog>{
    20  
    21     @Override
    22     public int compare(Dog o1, Dog o2) {
    23         return o1.weight - o2.weight;
    24     }
    25 }
    26  
    27 public class ArraySort {
    28  
    29     public static void main(String[] args) {
    30         Dog d1 = new Dog(2, 50);
    31         Dog d2 = new Dog(1, 30);
    32         Dog d3 = new Dog(3, 40);
    33  
    34         Dog[] dogArray = {d1, d2, d3};
    35         printDogs(dogArray);
    36  
    37         Arrays.sort(dogArray, new DogSizeComparator()); 
    38         printDogs(dogArray);
    39  
    40         Arrays.sort(dogArray, new DogWeightComparator());   
    41         printDogs(dogArray);
    42     }
    43  
    44     public static void printDogs(Dog[] dogs){
    45         for(Dog d: dogs)
    46             System.out.print("size="+d.size + " weight=" + d.weight + " ");
    47  
    48         System.out.println();
    49     }
    50 }

    输出:

    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”

    很显然,如果”Comparator<T>c”作为参数,但是第二个参数是”Comparator< ? super T > c”,使用<? super T>意味着类型可以是T或者是它的超类。为什么允许超类型呢?答案是:这种方式允许所有子类使用同一个comparator。看看下面这个例子一目了然。

     1 import java.util.Arrays;
     2 import java.util.Comparator;
     3  
     4 class Animal{
     5     int size;
     6 }
     7  
     8 class Dog extends Animal{
     9     public Dog(int s){
    10         size = s;
    11     }
    12 }
    13  
    14 class Cat extends Animal{
    15     public Cat(int s){
    16         size  = s;
    17     }
    18 }
    19  
    20 class AnimalSizeComparator implements Comparator<Animal>{
    21  
    22     @Override
    23     public int compare(Animal o1, Animal o2) {
    24         return o1.size - o2.size;
    25     }
    26     //in this way, all sub classes of Animal can use this comparator.
    27 }
    28  
    29 public class ArraySort {
    30  
    31     public static void main(String[] args) {
    32         Dog d1 = new Dog(2);
    33         Dog d2 = new Dog(1);
    34         Dog d3 = new Dog(3);
    35  
    36         Dog[] dogArray = {d1, d2, d3};
    37         printDogs(dogArray);
    38  
    39         Arrays.sort(dogArray, new AnimalSizeComparator());  
    40         printDogs(dogArray);
    41  
    42         System.out.println();
    43  
    44         //when you have an array of Cat, same Comparator can be used. 
    45         Cat c1 = new Cat(2);
    46         Cat c2 = new Cat(1);
    47         Cat c3 = new Cat(3);
    48  
    49         Cat[] catArray = {c1, c2, c3};
    50         printDogs(catArray);
    51  
    52         Arrays.sort(catArray, new AnimalSizeComparator());  
    53         printDogs(catArray);
    54     }
    55  
    56     public static void printDogs(Animal[] animals){
    57         for(Animal a: animals)
    58             System.out.print("size="+a.size + " ");
    59         System.out.println();
    60     }
    61 }

    输出:

    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()中你应该了解到:

    1. generic(范型)——super
    2. 策略模式
    3. 归并排序——nlog(n)时间复杂度
    4.  java.util.Collections.sort(List<T>list, Comparator<?super T> c)类似于Arrays.sort

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

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    ArraySortTest.java

     1 import java.util.Arrays;
     2 
     3 public class ArraySortTest {
     4     public static void main(String[] args) {
     5         int a[] = { 4, 32, 45, 32, 65, 32, 2 };
     6         System.out.print("数组排序前的顺序:");
     7         for (int i = 0; i < a.length; i++)
     8             System.out.print(a[i] + " ");
     9         Arrays.sort(a);// 数组的排序方法
    10         System.out.print("
    数组排序后的顺序:");
    11         for (int i = 0; i < a.length; i++)
    12             System.out.print(a[i] + " ");
    13         System.out.print("
    ");
    14 
    15     }
    16 }

    结果:

    数组排序前的顺序:4 32 45 32 65 32 2 
    数组排序后的顺序:2 4 32 32 32 45 65 

    ArraysSortTest2.java

     1 import java.util.Arrays;
     2 import java.util.Collections;
     3 
     4 public class ArraysSortTest2 {
     5     public static void main(String[] args) {
     6 
     7         String[] str = { "a", "e", "f", "g", "h", "i", "b", "c", "d" };
     8         System.out.println(".toString=" + Arrays.toString(str)); // 打印出数组中所有数据
     9         System.out.println(".asList=" + Arrays.asList(str));
    10 
    11         Arrays.sort(str);// 对数组进行排序
    12 
    13         System.out.println(".toString=" + Arrays.toString(str));// 打印排序后数组中所有数据
    14         System.out.println(".asList=" + Arrays.asList(str));
    15 
    16         Arrays.sort(str, Collections.reverseOrder());// 对数组进行 倒序
    17 
    18         System.out.println(".toString=" + Arrays.toString(str));// 打印排序后数组中所有数据
    19         System.out.println(".asList=" + Arrays.asList(str));
    20 
    21         int flag = Arrays.binarySearch(str, "a"); // 查找数组中 元素 的位置(数组下标从 0 开始)
    22         System.out.println("b的所在位置:" + flag);
    23 
    24         String[] str2 = new String[4];
    25         Arrays.fill(str2, "w");// 为数组中每个数据同初值
    26         System.out.println("str2[]=" + Arrays.toString(str2));
    27 
    28         String[][] s1 = { { "a", "b", "c", "d" }, { "a", "b", "e", "f" } };
    29         System.out.println("s1[][]=" + Arrays.deepToString(s1)); // 打印出二维数组中的全部数据
    30         //返回指定数组“深层内容”的字符串表示形式。
    31         //如果数组包含作为元素的其他数组,则字符串表示形式包含其内容等。此方法是为了将多维数组转换为字符串而设计的。 
    32     }
    33 }

     结果:

    .toString=[a, e, f, g, h, i, b, c, d]
    .asList=[a, e, f, g, h, i, b, c, d]
    
    .toString=[a, b, c, d, e, f, g, h, i]
    .asList=[a, b, c, d, e, f, g, h, i]
    
    .toString=[i, h, g, f, e, d, c, b, a]
    .asList=[i, h, g, f, e, d, c, b, a]
    
    b的所在位置:-1
    str2[]=[w, w, w, w]
    s1[][]=[[a, b, c, d], [a, b, e, f]]

     附上API文档 从Arrays这个名字中就可以看出Arrays中的的方法都是服务于数组Array的

  • 相关阅读:
    框架集。样式表
    2017.11.23知识点整理
    HTML5的标签
    HTML5大体概括截图
    2017.11.21 通用标签及属性
    2017.11.21 课程随记
    JavaScript数组
    JavaScript语句
    javascript基础知识
    不用alert提示的非空表单验证
  • 原文地址:https://www.cnblogs.com/DreamDrive/p/4284719.html
Copyright © 2011-2022 走看看