好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

深入了解Java核心类库--Arrays类

Java常用类库Arrays

类Arrays包含用于操作数组的各种方法(例如排序和搜索)

如果指定的数组引用为null,则此类中的方法都抛出NullPointerException ,除非另有说明

一、常用方法

1.1 toString

返回指定数组内容的 字符串形式

举例

?

1

2

int [] a1 = { 1 , 2 , 3 , 4 , 5 };

System.out.println(Arrays.toString(a1)); //[1, 2, 3, 4, 5]

源码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

public static String toString( int [] a) {

     if (a == null )

         return "null" ;

     int iMax = a.length - 1 ;

     if (iMax == - 1 )

         return "[]" ;

     StringBuilder b = new StringBuilder();

     b.append( '[' );

     for ( int i = 0 ; ; i++) {

         b.append(a[i]);

         if (i == iMax)

             return b.append( ']' ).toString();

         b.append( ", " );

     }

}

其它

Modifier and Type Field Description
static String deepToString​(Object[] a) 返回指定数组的[深层内容]的字符串表示形式

1.2 Sort

排序(默认升序)

1.2.1 sort​(T[] a, int fromIndex, int toIndex)

指定区间进行排序

举例

?

1

2

3

4

int [] a1 = { 9 , 1 , 3 , 7 , 2 , 5 };

System.out.println(Arrays.toString(a1)); //[9, 1, 3, 7, 2, 5]

Arrays.sort(a1, 0 , 3 ); //[0,3),对9,1,3进行排序

System.out.println(Arrays.toString(a1)); //[0[1, 3, 9, 7, 2, 5]

源码

?

1

2

3

4

public static void sort( int [] a, int fromIndex, int toIndex) {

     rangeCheck(a.length, fromIndex, toIndex);

     DualPivotQuicksort.sort(a, fromIndex, toIndex - 1 , null , 0 , 0 );

}

1.2.2 Sort(T[] a)

对整个数组进行排序

举例

?

1

2

3

4

int [] a1 = { 0 , 7 , 8 , 2 , 4 , 1 };

System.out.println(Arrays.toString(a1)); //[0, 7, 8, 2, 4, 1]

Arrays.sort(a1);

System.out.println(Arrays.toString(a1)); //[0, 1, 2, 4, 7, 8]

源码

?

1

2

public static void sort( int [] a) { DualPivotQuicksort.sort(a, 0 , a.length - 1 , null , 0 , 0 );

}

1.2.3 其它 Modifier and Type Field Description
static void sort​(T[] a, int fromIndex, int toIndex, Comparator<? super T> c) 根据指定比较器引发的顺序对指定对象数组的指定范围进行排序。
static void sort​(T[] a, Comparator<? super T> c) 根据指定比较器引发的顺序对指定的对象数组进行排序。
     
     
static void parallelSort​(T[] a) 将指定的数组按升序排序。
static void parallelSort​(T[] a, int fromIndex, int toIndex) 将指定的数组范围按数字升序排序。
static <T extends Comparable<? super T>>void parallelSort​(T[] a) 根据元素的natural ordering对指定的对象数组按升序排序。
static <T extends Comparable<? super T>>void parallelSort​(T[] a, int fromIndex, int toIndex) 根据元素的natural ordering ,将指定对象数组的指定范围按升序排序。
static void parallelSort​(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp) 根据指定比较器引发的顺序对指定对象数组的指定范围进行排序。
static void parallelSort​(T[] a, Comparator<? super T> cmp) 根据指定比较器引发的顺序对指定的对象数组进行排序。

1.3 copyOf

复制(常用于数组扩容)

举例

?

1

2

3

4

int [] a = { 1 , 2 , 3 };

System.out.println(a.length); //output:3

a = Arrays.copyOf(a, 15 );

System.out.println(a.length); //output:15

源码

?

1

2

3

4

5

public static int [] copyOf( int [] original, int newLength) {

       int [] copy = new int [newLength];

       System.arraycopy(original, 0 , copy, 0 , Math.min(original.length, newLength));

       return copy;

   }

其它

Modifier and Type Field Description
static T[] copyOf​(T[] original, int newLength) 使用空值复制指定的数组,截断或填充(如有必要),以使副本具有指定的长度
static <T,​U>T[] copyOf​(U[] original, int newLength, 类<? extends T[]> newType) 使用空值复制指定的数组,截断或填充(如有必要),以使副本具有指定的长度

1.4 mismatch

举例

?

1

2

3

4

5

int [] a1 = { 0 , 1 , 2 , 3 , 4 , 5 };

int [] a2 = { 0 , 1 , 2 , 3 , 4 , 5 }; //与a1相同

int [] a3 = { 0 , 1 , 2 , 3 , 0 , 5 }; //从索引4开始与a1不同

System.out.println(Arrays.mismatch(a1,a2)); //output:-1

System.out.println(Arrays.mismatch(a1,a3)); //output:4

源码

?

1

2

3

4

5

6

7

public static int mismatch( int [] a, int [] b) {

        int length = Math.min(a.length, b.length); // Check null array refs

        if (a == b)

            return - 1 ;

        int i = ArraysSupport.mismatch(a, b, length);

        return (i < 0 && a.length != b.length) ? length : i;

    }

其它

Modifier and Type Field Description
static int mismatch​(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) 查找并返回指定范围内两个 Object数组之间第一个不匹配的相对索引,否则如果未找到不匹配则返回-1。
static int mismatch​(T[] a, T[] b, Comparator<? super T> cmp) 查找并返回两个 Object数组之间第一个不匹配的索引,否则如果未找到不匹配则返回-1。

1.5 binarySearch

二分查找,搜索,返回下标

1.5.1 binarySearch​(T[] a, int fromIndex, int toIndex, T key)

限定了搜索的范围[fromIndex, toIndex)

举例

?

1

2

3

int [] a = { 1 , 2 , 3 , 4 , 5 };

      int x1 = Arrays.binarySearch(a, 2 , 3 , 4 ); //在a数组下标[2,3)中查找值为4的下标

      System.out.println(x1); //output:<0的随机数

源码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

public static int binarySearch( int [] a, int fromIndex, int toIndex, int key) {

     rangeCheck(a.length, fromIndex, toIndex);

     return binarySearch0(a, fromIndex, toIndex, key);

}

private static int binarySearch0( int [] a, int fromIndex, int toIndex, int key) {

     int low = fromIndex;

     int high = toIndex - 1 ;

     while (low <= high) {

         int mid = (low + high) >>> 1 ;

         int midVal = a[mid];

         if (midVal < key)

             low = mid + 1 ;

         else if (midVal > key)

             high = mid - 1 ;

         else

             return mid; // key found

     }

     return -(low + 1 );  // key not found.

}

1.5.2 binarySearch​(T[] a, T key)

与上述相同,只是没有限定范围,fromIndex=0, toIndex=length

举例

?

1

2

3

4

5

int [] a = { 1 , 2 , 3 , 4 , 5 };

int x1 = Arrays.binarySearch(a, 3 ); //在a数组中查找值为3的下标

int x2 = Arrays.binarySearch(a,- 6 ); //在a数组中查找值为6的下标

System.out.println(x1); //output:2

System.out.println(x2); //output:<0的随机数

源码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

public static int binarySearch( int [] a, int key) {

     return binarySearch0(a, 0 , a.length, key);

}

private static int binarySearch0( int [] a, int fromIndex, int toIndex, int key) {

     int low = fromIndex;

     int high = toIndex - 1 ;

     while (low <= high) {

         int mid = (low + high) >>> 1 ;

         int midVal = a[mid];

         if (midVal < key)

             low = mid + 1 ;

         else if (midVal > key)

             high = mid - 1 ;

         else

             return mid; // key found

     }

     return -(low + 1 );  // key not found.

}

1.5.3 其它 Modifier and Type Field Description
static int binarySearch​(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c) 使用二进制搜索算法搜索指定对象的指定数组范围
static int binarySearch​(T[] a, T key, Comparator<? super T> c) 使用二进制搜索算法在指定的数组中搜索指定的对象

1.6 equals

1.6.1 equals​(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex)

如果两个指定数组在指定范围内相等 ,则返回true

举例

?

1

2

3

4

int [] a1 = { 1 , 2 , 3 , 4 , 5 };

int [] a2 = { 1 , 2 , 0 , 0 , 4 , 5 };

System.out.println(Arrays.equals(a1, 0 , 2 ,a2, 0 , 2 )); //true

System.out.println(Arrays.equals(a1, 3 , 5 ,a2, 4 , 6 )); //true

源码

?

1

2

3

4

5

6

7

8

9

public static boolean equals( int [] a, int aFromIndex, int aToIndex, int [] b, int bFromIndex, int bToIndex) {

         rangeCheck(a.length, aFromIndex, aToIndex);

         rangeCheck(b.length, bFromIndex, bToIndex);

         int aLength = aToIndex - aFromIndex;

         int bLength = bToIndex - bFromIndex;

         if (aLength != bLength)

             return false ;

         return ArraysSupport.mismatch(a, aFromIndex, b, bFromIndex,aLength) < 0 ;

     }

1.6.2 equals​(T[] a, T[] a2)

如果两个指定数组相等,则返回 true

举例

?

1

2

3

4

5

int [] a1 = { 1 , 2 , 3 , 4 , 5 };

int [] a2 = { 1 , 2 , 3 , 4 , 5 };

int [] a3 = { 1 , 2 , 0 , 4 , 5 };

System.out.println(Arrays.equals(a1,a2)); //true

System.out.println(Arrays.equals(a1,a3)); //false

源码

?

1

2

3

4

5

6

7

8

9

10

public static boolean equals( int [] a, int [] a2) {

        if (a==a2)

            return true ;

        if (a== null || a2== null )

            return false ;

        int length = a.length;

        if (a2.length != length)

            return false ;

        return ArraysSupport.mismatch(a, a2, length) < 0 ;

    }

1.6.3 其它 Modifier and Type Field Description
static boolean deepEquals​(Object[] a1, Object[] a2) 如果两个指定的数组彼此 深度相等 ,则返回 true
static boolean equals​(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) 如果在指定范围内指定的两个Object数组彼此 相等 ,则返回true
static boolean equals​(T[] a, T[] a2, Comparator<? super T> cmp) 如果两个指定的Objects数组彼此 相等 ,则返回 true

1.7 fills

1.7.1 fill​(T[] a, int fromIndex, int toIndex, T val)

将指定的T值分配给指定的T类型数组的指定范围的每个元素

举例

?

1

2

3

4

5

6

int [] a1 = new int [ 10 ];

Arrays.fill(a1, 1 , 4 , 8 );

char [] a2 = new char [ 10 ];

Arrays.fill(a2, 0 , 3 , 's' );

System.out.println(Arrays.toString(a1)); //[0, 8, 8, 8, 0, 0, 0, 0, 0, 0]

System.out.println(Arrays.toString(a2)); //[s, s, s,  ,  ,  ,  ,  ,  ,  ]

源码

?

1

2

3

4

5

public static void fill( char [] a, int fromIndex, int toIndex, char val) {

     rangeCheck(a.length, fromIndex, toIndex);

     for ( int i = fromIndex; i < toIndex; i++)

         a[i] = val;

}

1.7.2 fill​(T[] a, T val)

将指定的T值分配给指定的T类型数组的每个元素

举例

?

1

2

3

4

5

6

int [] a1 = new int [ 10 ];

Arrays.fill(a1, 8 );

char [] a2 = new char [ 10 ];

Arrays.fill(a2, 's' );

System.out.println(Arrays.toString(a1)); //[8, 8, 8, 8, 8, 8, 8, 8, 8, 8]

System.out.println(Arrays.toString(a2)); //[s, s, s, s, s, s, s, s, s, s]

源码

?

1

2

3

4

public static void fill( int [] a, int val) {

     for ( int i = 0 , len = a.length; i < len; i++)

         a[i] = val;

}

二、其他方法

Modifier and Type Field Description
static List asList​(T… a) 返回由指定数组支持的固定大小的列表。
     
     
static int compare​(T[] a, T[] b) 字典顺序比较两个T阵列
static int compare​(T[] a, int aFromIndex, int aToIndex,T[] b, int bFromIndex, int bToIndex) 在指定范围内按字典顺序比较两个T阵列
static <T extends Comparable<? super T>>int compare​(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) 在指定范围内按字典顺序比较两个 Object阵列。
static int compare​(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) 在指定范围内按字典顺序比较两个 Object阵列。
static <T extends Comparable<? super T>>int compare​(T[] a, T[] b) 按 Object顺序比较两个 Object阵列,在可比元素中。
static int compare​(T[] a, T[] b, Comparator<? super T> cmp) 使用指定的比较器按字典顺序比较两个 Object阵列
     
     
static int compareUnsigned​(T[] a, T[] b) byte字典顺序比较两个T阵列,数字处理元素为无符号
static int compareUnsigned​(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) 在指定范围内按字典顺序比较两个 T阵列,将元素数字处理为无符号
     
     
static T[] copyOfRange​(T[] original, int from, int to) 将指定数组的指定范围复制到新数组中
static T[] copyOfRange​(T[] original, int from, int to) 将指定数组的指定范围复制到新数组中
static <T,​U>T[] copyOfRange​(U[] original, int from, int to, 类<? extends T[]> newType) 将指定数组的指定范围复制到新数组中
     
     
static int hashCode​(T[] a) 根据指定数组的内容返回哈希码
static int deepHashCode​(Object[] a) 返回基于指定数组的[深层内容]的哈希码
     
     
static void parallelPrefix​(T[] array, int fromIndex, int toIndex,TBinaryOperator op) 对于给定的数组子范围执行parallelPrefix(T[], TBinaryOperator)
static void parallelPrefix​(dT[] array, TBinaryOperator op) 使用提供的函数并行地累积给定数组的每个元素
static void parallelPrefix​(T[] array, int fromIndex, int toIndex, BinaryOperator op) 对于给定的数组子范围执行 parallelPrefix(Object[], BinaryOperator)
static void parallelPrefix​(T[] array, BinaryOperator op) 使用提供的函数并行地累积给定数组的每个元素
     
     
static void parallelSetAll​(double[] array, IntToDoubleFunction generator) 使用提供的生成器函数并行设置指定数组的所有元素以计算每个元素
static void parallelSetAll​(int[] array, IntUnaryOperator generator) 使用提供的生成器函数并行设置指定数组的所有元素以计算每个元素
static void parallelSetAll​(long[] array, IntToLongFunction generator) 使用提供的生成器函数并行设置指定数组的所有元素以计算每个元素
static void parallelSetAll​(T[] array, IntFunction<? extends T> generator) 使用提供的生成器函数并行设置指定数组的所有元素以计算每个元素
     
     
static void setAll​(double[] array, IntToDoubleFunction generator) 使用提供的生成器函数设置指定数组的所有元素以计算每个元素
static void setAll​(int[] array, IntUnaryOperator generator) 使用提供的生成器函数设置指定数组的所有元素以计算每个元素
static void setAll​(long[] array, IntToLongFunction generator) 使用提供的生成器函数设置指定数组的所有元素以计算每个元素
static void setAll​(T[] array, IntFunction<? extends T> generator) 使用提供的生成器函数设置指定数组的所有元素以计算每个元素
     
     
static Spliterator.OfDouble spliterator​(double[] array) 返回覆盖所有指定数组的Spliterator.OfDouble
static Spliterator.OfDouble spliterator​(double[] array, int startInclusive, int endExclusive) 返回覆盖指定数组的指定范围的Spliterator.OfDouble
static Spliterator.OfInt spliterator​(int[] array) 返回覆盖所有指定数组的Spliterator.OfInt
static Spliterator.OfInt spliterator​(int[] array, int startInclusive, int endExclusive) 返回覆盖指定数组的指定范围的Spliterator.OfInt
static Spliterator.OfLong spliterator​(long[] array) 返回覆盖所有指定数组的Spliterator.OfLong
static Spliterator.OfLong spliterator​(long[] array, int startInclusive, int endExclusive) 返回覆盖指定数组的指定范围的Spliterator.OfLong
static Spliterator spliterator​(T[] array) 返回覆盖所有指定数组的Spliterator
static Spliterator spliterator​(T[] array, int startInclusive, int endExclusive) 返回覆盖指定数组的指定范围的Spliterator
static DoubleStream stream​(double[] array) 返回以指定数组作为源的顺序DoubleStream
static DoubleStream stream​(double[] array, int startInclusive, int endExclusive) 返回指定数组的指定范围作为其源的顺序DoubleStream
static IntStream stream​(int[] array) 返回以指定数组作为源的顺序IntStream
static IntStream stream​(int[] array, int startInclusive, int endExclusive) 返回指定数组的指定范围作为其源的顺序IntStream
static LongStream stream​(long[] array) 返回以指定数组作为源的顺序LongStream
static LongStream stream​(long[] array, int startInclusive, int endExclusive) 返回指定数组的指定范围作为其源的顺序LongStream
static Stream stream​(T[] array) 返回以指定数组作为源的顺序Stream
static Stream stream​(T[] array, int startInclusive, int endExclusive) 返回指定数组的指定范围作为其源的顺序Stream

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注的更多内容!

原文链接:https://blog.csdn.net/m0_50609545/article/details/117884151

查看更多关于深入了解Java核心类库--Arrays类的详细内容...

  阅读:16次