Java7/8集合框架——集合工具类Arrays内部方法浅析

java.util.Arrays

备注:本文只对Java7中的java.util.Arrays中提供的基本功能进行了一次大致的介绍,并没有对其具体的实现原理进行深入的探讨和分析。详情可自己深入观摩源码。

本文的主要结构:

  1. 开场:API的经典注释
  2. 数组排序:sort
  3. 二分法查找数组中的元素:binarySearch
  4. 比较两个数组是否相等:equals
  5. 填数组填充元素:fill,充对数组中的指定位置填充相同的元素内容
  6. 数组拷贝:copyOf
  7. 数组转链表:asList,将数组转换为一个固定的List链表对象
  8. 计算数组的哈希值:hashCode
  9. 数组转String:toString,以特定格式输出数组

开场API的经典注释

  先来一段官方注释:


 This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.

Arrays这个类提供了用于操作数组的不同方法(例如:排序和查找),也包含了一个静态工厂,用于允许数组当作链表进行操作。

The methods in this class all throw a NullPointerException, if the specified array reference is null, except where noted.

除了有特殊注释的,这个类的所有方法都会在数组引用是空时抛出“NullPointerException”空指针异常。


 这个注释也是为这个工具类的作用定了基调,其作用基本如下,即注释所体现的:

  1. 用于操作数组的方法:排序、二分查找、比较判断、填充、数组拷贝、数组转链表、数组hashCode、数组转字符串String等
  2. 静态工厂:将数组转化为链表进行操作

一、数组排序:sort

  主要是对基本类型的数组进行排序,排序算法主要是Dual-Pivot Quicksort(双基准快排,暂且未研究,可以百度/谷歌了解下),而非经典的快排(one-pivot单基准)。

  在可以对原数组元素顺序进行改变时,可以使用这些现成的排序工具方法。

 1 // 对int型数组进行排序,会改变原数组中的元素位置
 2 public static void sort(int[] a){...}
 3 // 对数组fromIndex下标(包含)到toIndex下标(不包含)的元素进行排序,同样会改变原数组的元素
 4 public static void sort(int[] a, int fromIndex, int toIndex){...}
 5 // long型数组的排序
 6 public static void sort(long[] a){...}
 7 public static void sort(long[] a, int fromIndex, int toIndex){...}
 8 // short型数组的排序
 9 public static void sort(short[] a){...}
10 public static void sort(short[] a, int fromIndex, int toIndex){...}
11 // char型数组的排序
12 public static void sort(char[] a){...}
13 public static void sort(char[] a, int fromIndex, int toIndex){...}
14 // byte型数组的排序
15 public static void sort(byte[] a){...}
16 public static void sort(byte[] a, int fromIndex, int toIndex){...}
17 // float型数组的排序
18 public static void sort(float[] a){...}
19 public static void sort(float[] a, int fromIndex, int toIndex){...}
20 // double型数组的排序
21 public static void sort(double[] a){...}
22 public static void sort(double[] a, int fromIndex, int toIndex){...}

   还有Object数组和泛型(需要提供Comparator)的比较排序,用的是归并排序。

二、二分法查找数组中的元素:binarySearch

  主要是在一排序的基本类型数组中进行二分查找,找不到指定元素时返回下标值小于0。

 1 // int排序数组中的二分查找
 2 public static int binarySearch(int[] a, int key){...}
 3 // 对int数组fromIndex下标数组fromIndex下标(包含)到toIndex下标(不包含)的元素的二分查找
 4 public static int binarySearch(int[] a, int fromIndex, int toIndex, int key){...}
 5 // short排序数组中的二分查找
 6 public static int binarySearch(short[] a, short key){...}
 7 public static int binarySearch(short[] a, int fromIndex, int toIndex, short key){...}
 8 // char排序数组中的二分查找
 9 public static int binarySearch(char[] a, char key){...}
10 public static int binarySearch(char[] a, int fromIndex, int toIndex, char key){...}
11 // byte排序数组中的二分查找
12 public static int binarySearch(byte[] a, byte key){...}
13 public static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key){...}
14 // double排序数组中的二分查找
15 public static int binarySearch(double[] a, double key){...}
16 public static int binarySearch(double[] a, int fromIndex, int toIndex, double key){...}
17 // float排序数组中的二分查找
18 public static int binarySearch(float[] a, float key){...}
19 public static int binarySearch(float[] a, int fromIndex, int toIndex, float key){...}

   还有对Object数组和泛型(需要提供Comparator)的二分查找。 

三、比较两个数组是否相等:equals

  判断两个数组是否相等。

 1 // long数组判断
 2 public static boolean equals(long[] a, long[] a2){...}
 3 // int数组判断
 4 public static boolean equals(int[] a, int[] a2){...}
 5 // short数组判断
 6 public static boolean equals(short[] a, short a2[]){...}
 7 // char数组判断
 8 public static boolean equals(char[] a, char[] a2){...}
 9 // byte数组判断
10 public static boolean equals(byte[] a, byte[] a2){...}
11 // boolean数组判断
12 public static boolean equals(boolean[] a, boolean[] a2){...}
13 // double数组判断
14 public static boolean equals(double[] a, double[] a2){...}
15 // float数组判断
16 public static boolean equals(float[] a, float[] a2){...}
17 // Object数组判断,主要使用equals进行判断
18 public static boolean equals(Object[] a, Object[] a2){...}

四、填数组填充元素:fill,充对数组中的指定位置填充相同的元素内容

   对数组(指定下标或者全部)填充某指定元素

 1 // long数组
 2 public static void fill(long[] a, long val){...}
 3 public static void fill(long[] a, int fromIndex, int toIndex, long val){...}
 4 // int数组
 5 public static void fill(int[] a, int val){...}
 6 public static void fill(int[] a, int fromIndex, int toIndex, int val){...}
 7 // short数组
 8 public static void fill(short[] a, short val){...}
 9 public static void fill(short[] a, int fromIndex, int toIndex, short val){...}
10 // char数组
11 public static void fill(char[] a, char val){...}
12 public static void fill(char[] a, int fromIndex, int toIndex, char val){...}
13 // byte数组
14 public static void fill(byte[] a, byte val){...}
15 public static void fill(byte[] a, int fromIndex, int toIndex, byte val){...}
16 // boolean数组
17 public static void fill(boolean[] a, boolean val){...}
18 public static void fill(boolean[] a, int fromIndex, int toIndex, boolean val){...}
19 // double数组
20 public static void fill(double[] a, double val){...}
21 public static void fill(double[] a, int fromIndex, int toIndex,double val){...}
22 // float数组
23 public static void fill(float[] a, float val){...}
24 public static void fill(float[] a, int fromIndex, int toIndex, float val){...}
25 // Object数组
26 public static void fill(Object[] a, Object val){...}
27 public static void fill(Object[] a, int fromIndex, int toIndex, Object val){...}

五、数组拷贝:copyOf

  数组的复制,还有Object和泛型的。

// 数组的全复制
public static byte[] copyOf(byte[] original, int newLength){...}
public static short[] copyOf(short[] original, int newLength){...}
public static int[] copyOf(int[] original, int newLength){...}
public static long[] copyOf(long[] original, int newLength){...}
public static char[] copyOf(char[] original, int newLength){...}
public static float[] copyOf(float[] original, int newLength){...}
public static double[] copyOf(double[] original, int newLength){...}
public static boolean[] copyOf(boolean[] original, int newLength){...}
// 从from下标(包含)到to下标(不包含)的复制版本
public static short[] copyOfRange(xxx[] original, int from, int to){...}

  

六、数组转链表:asList,将数组转换为一个固定的List链表对象

  将数组转换成链表形式返回,返回的是内部类ArrayList。对于这个链表,更改操作只有set,而且会改变原来的数组内容;另外就是不支持add操作,对于add调用,会抛出UnsupportedOperationException异常(没有重写add实现时的默认实现)。

1 public static <T> List<T> asList(T... a){...}

  

七、计算数组的哈希值:hashCode

  计算数组的hashCode。

1 // 对数组进行hash
2 public static int hashCode(float a[]){...}

八、数组转String:toString,以特定格式输出数组

  将数组转化为String,格式为:[x,xx,xxx,xxxx],若是数组为空,返回null;若是对象数组中某元素为空,使用null代替;空

1 // 返回格式为:[x,xx,xxx,xxxx]
2 public static String toString(xxx[] a){...}

猜你喜欢

转载自www.cnblogs.com/wpbxin/p/9248594.html