Java java.util.Arrays类相关方法示例

x33g5p2x  于2021-08-20 转载在 Java  
字(13.2k)|赞(0)|评价(0)|浏览(414)

###Arrays类概述

java.util.Arrays类包含一个静态工厂,它允许将数组视为列表。以下是关于Arrays的重要观点

  1. 该类包含各种操作数组的方法(如排序和搜索)。
  2. 如果指定的数组引用为空,该类中的方法会抛出一个NullPointerException。

类声明

以下是java.util.Arrays类的声明 -

public class Arrays
   extends Object

java.util.Arrays类的API

java.util.Arrays类提供了很多方法,但在本指南中,我们将介绍经常使用的和重要的Arrays类方法。

1. 将数组转换为列表

  • static List asList(T... a) - 返回一个由指定数组支持的固定大小的列表。

2. 搜索一个数组

  • static int binarySearch(byte[] a, byte key) - 使用二进制搜索算法在指定的字节阵列中搜索指定的值。
  • static int binarySearch(char[] a, char key) - 使用二进制搜索算法在指定的字符数组中搜索指定的值。
  • static int binarySearch(double[] a, double key) - 使用二进制搜索算法在指定的二进制数组中搜索指定的值。
  • static int binarySearch(float[] a, float key) - 使用二进制搜索算法在指定的浮点数阵列中搜索指定的值。
  • static int binarySearch(int[] a, int key) - 使用二进制搜索算法在指定的ints阵列中搜索指定的值。
  • static int binarySearch(long[] a, long key) - 使用二进制搜索算法在指定的长数组中搜索指定的值。
  • static int binarySearch(Object[] a, Object key) - 使用二进制搜索算法在指定的数组中搜索指定的对象。
  • static int binarySearch(short[] a, short key) - 使用二进制搜索算法在指定的短线阵列中搜索指定的值。
  • static int binarySearch(T[] a, T key, Comparator<? super T> c) - 使用二进制搜索算法在指定的数组中搜索指定的对象。

3. 复制一个数组

  • static int[] copyOf(int[] original, int newLength) - 复制指定的数组,截断或用零填充(如果需要),使副本具有指定的长度。

4. 检查两个数组是否深度相等

  • static boolean deepEquals(Object[] a1, Object[] a2) - 如果两个指定的数组彼此深度相等,则返回true。

5. 填充数组中的数值

  • static void fill(int[] a, int val) - 将指定的int值分配给指定的ints数组的每个元素。

6. 对数组进行排序

  • static void sort(byte[] a) - 将指定的数组排序为升序数字。
  • static void sort(byte[] a, int fromIndex, int toIndex) - 将指定的数组范围按升序排序。
  • static void sort(char[] a) - 将指定的数组按升序数字排序。
  • static void sort(char[] a, int fromIndex, int toIndex) - 将指定的数组范围以升序排序。
  • static void sort(double[] a) - 将指定的数组按升序数字排序。
  • static void sort(double[] a, int fromIndex, int toIndex) - 将数组的指定范围以升序排序。
  • static void sort(float[] a) - 将指定的数组以升序的数字排序。
  • static void sort(float[] a, int fromIndex, int toIndex) - 将指定的数组范围以升序排序。
  • static void sort(int[] a) - 将指定的数组以升序数字排序。
  • static void sort(int[] a, int fromIndex, int toIndex) - 将指定的数组范围以升序排序。
  • static void sort(long[] a) - 将指定的数组以升序数字排序。
  • static void sort(long[] a, int fromIndex, int toIndex) - 将指定的数组范围以升序排序。
  • static void sort(Object[] a) - 根据元素的自然排序,将指定的数组对象排序为升序。
  • static void sort(Object[] a, int fromIndex, int toIndex) - 根据元素的自然排序,将指定的对象数组的指定范围排序为升序。
  • static void sort(short[] a) - 将指定的数组排序为升序的数字顺序。
  • static void sort(short[] a, int fromIndex, int toIndex) - 将指定的数组范围以升序排序。
  • static void sort(T[] a, Comparator<? super T> c) - 根据指定的比较器引起的顺序对指定的数组对象进行排序。
  • static void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c) - 根据指定的比较器引起的顺序对指定的对象数组的指定范围排序。

7. 数组的字符串表示法

  • static String toString(boolean[] a) - 返回指定数组内容的字符串表示。
  • static String toString(byte[] a) - 返回指定数组内容的字符串表示。
  • static String toString(char[] a) - 返回指定数组内容的字符串表示。
  • static String toString(double[] a) - 返回指定数组内容的字符串表示。
  • static String toString(float[] a) - 返回指定数组内容的字符串表示。
  • static String toString(int[] a) - 返回指定数组内容的字符串表示。
  • static String toString(long[] a) - 返回指定数组内容的字符串表示。
  • static String toString(Object[] a) - 返回指定数组内容的字符串表示。
  • static String toString(short[] a) - 返回指定数组内容的字符串表示。

1. 将数组转换为列表

  • static List asList(T... a) - 返回一个由指定数组支持的固定大小的列表。
    让我们使用asList()来将数组转换为ArrayList。

例子。在这个例子中,我们将把字符串数组转换为字符串类型的ArrayList。

import java.util.Arrays;
import java.util.List;

//*/*
/* This class shows different methods to convert Array to ArrayList
/* 
/* @author javaguides.net
/*
/*/
public class ArrayToArrayList {

        public static void main(String[] args) {
                String anArrayOfStrings[] = { "Agra", "Mysore", "Chandigarh", "Bhopal" };

                List<String> strList = Arrays.asList(anArrayOfStrings);

                System.out.println("Original ArrayList from Arrays.asList()");

                //* Display array list /*/
                strList.forEach(str -> System.out.println(" " + str));

                // change the array element and see the effect is propogated to list
                // also.
                anArrayOfStrings[0] = "Dehli";

                System.out.println("\nChange in array effect on ArrayList");

                //* Display array list /*/
                strList.forEach(str -> System.out.println(" " + str));
        }

}

输出

Original ArrayList from Arrays.asList()
 Agra
 Mysore
 Chandigarh
 Bhopal

Change in array effect on ArrayList
 Dehli
 Mysore
 Chandigarh
 Bhopal

让我们再看一个例子,将整数数组转换为整数类型的ArrayList。

Integer anArrayOfIntegers[] = { 1,2,3,4,5,6 };

List<Integer> intList = Arrays.asList(anArrayOfIntegers);

//* Display array list /*/
intList.forEach(str -> System.out.println(" " + str));

输出

1
 2
 3
 4
 5
 6

2. 搜索一个数组

Arrays类提供了许多重载的search()方法,使用二进制搜索算法在指定的数组中搜索指定的对象。

  • static int binarySearch(byte[] a, byte key) - 使用二进制搜索算法在指定的字节阵列中搜索指定的值。
  • static int binarySearch(char[] a, char key) - 使用二进制搜索算法在指定的字符数组中搜索指定的值。
  • static int binarySearch(double[] a, double key) - 使用二进制搜索算法在指定的二进制数组中搜索指定的数值。
  • static int binarySearch(float[] a, float key) - 使用二进制搜索算法在指定的浮点数阵列中搜索指定的值。
  • static int binarySearch(int[] a, int key) - 使用二进制搜索算法在指定的ints阵列中搜索指定的值。
  • static int binarySearch(long[] a, long key) - 使用二进制搜索算法在指定的长数组中搜索指定的值。
  • static int binarySearch(Object[] a, Object key) - 使用二进制搜索算法在指定的数组中搜索指定的对象。
  • static int binarySearch(short[] a, short key) - 使用二进制搜索算法在指定的短线阵列中搜索指定的值。
  • static int binarySearch(T[] a, T key, Comparator<? super T> c) - 使用二进制搜索算法在指定的数组中搜索指定的对象。
    例子。这个例子演示了上述所有搜索方法的用法。
import java.util.Arrays;
import java.util.Date;
import java.util.List;

//*/*
/* java.util.Arrays Class API examples
/* 
/* @author javaguides.net
/*
/*/
public class ArraysJavaUtilClass {

        public static void main(String[] args) {

                // Searches the specified array for the specified String using the
                // binary search algorithm.
                final String key = "abc";
                String[] strArray = { "abc", "cdf", "pqr" };
                int index = Arrays.binarySearch(strArray, key);
                System.out.println(" String key found at index : " + index);

                // Searches the specified array of ints for the specified value using
                // the binary search algorithm.
                int[] intArray = { 1, 2, 3, 4 };
                index = Arrays.binarySearch(intArray, 3);
                System.out.println(" String key found at index : " + index);
                // Searches the specified array of bytes for the specified value using
                // the binary search algorithm.
                byte k = 1;
                byte[] byteArray = { 1, 2, 3, 4, 5 };
                Arrays.binarySearch(byteArray, k);

                // Searches the specified array of chars for the specified value using
                // the binary search algorithm.
                char charKey = 'a';
                char[] charArray = { 'a', 'b', 'c' };
                Arrays.binarySearch(charArray, charKey);

                // Searches the specified array of doubles for the specified value using
                // the binary search algorithm.
                double[] doubleArray = { 0.1, 0.2, 0.3 };
                Arrays.binarySearch(doubleArray, 0.2);

                // Searches the specified array of longs for the specified value using
                // the binary search algorithm.
                long[] longArray = { 1, 2, 3, 4, 5 };
                Arrays.binarySearch(longArray, 1);

                // Searches the specified array of floats for the specified value using
                // the binary search algorithm
                float[] floatArray = { 1, 2, 3, 4, 5 };
                Arrays.binarySearch(floatArray, 2);
                
        }
}

3. 复制一个数组

  • static int[] copyOf(int[] original, int newLength) - 复制指定的数组,截断或用零填充(如果需要),使副本具有指定的长度。
    如果你想复制一个数组的前几个元素或数组的完整副本,你可以使用这个方法。很明显,它不像System.arraycopy()那样用途广泛,但是它也不容易混淆,而且很容易使用。这个方法在内部使用System arraycopy()方法。
import java.util.Arrays;
//*/*
/* This class shows different methods for copy array in java
/* @author javaguides.net
/*
/*/
public class JavaArrayCopyExample {

        public static void main(String[] args) {
                int[] source = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                
                System.out.println("Source array = " + Arrays.toString(source));

                int[] dest = Arrays.copyOf(source, source.length);
                
                System.out.println(
                                "Copy First five elements of array. Result array = " + Arrays.toString(dest));
       }
}

输出:

Source array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Copy First five elements of array. Result array = [1, 2, 3, 4, 5, 6, 7, 8, 9] 

4. 检查两个数组是否深度相等

  • static boolean deepEquals(Object[] a1, Object[] a2) - 如果指定的两个数组彼此深度相等,则返回true。
String[] strArray1 = { "abc", "cdf", "pqr" };
String[] strArray2 = { "abc", "cdf", "pqr" };
System.out.println("Two Arrays Deep Equals ::  " + Arrays.deepEquals(strArray1, strArray2));

输出

Two Arrays Deep Equals ::  true

5. 填充数组中的值

  • static void fill(int[] a, int val) - 将指定的int值分配给指定的ints数组的每个元素。
public class ArraysJavaUtilClass {

        public static void main(String[] args) {
                // Assigns the specified int value to each element of the specified
                // array of ints.
                int[] fillArray = new int[5];
                System.out.printf("fillArray (before): %s\n", Arrays.toString(fillArray));
                Arrays.fill(fillArray, 1);
                System.out.printf("fillArray (after): %s", Arrays.toString(fillArray));
                
        }
}

输出

fillArray (before): [0, 0, 0, 0, 0]
fillArray (after): [1, 1, 1, 1, 1]

6. 对一个数组进行排序

  • static void sort(byte[] a) - 将指定的数组排序为升序数字。
  • static void sort(byte[] a, int fromIndex, int toIndex) - 对指定的数组范围进行升序排序。
  • static void sort(char[] a) - 将指定的数组按升序数字排序。
  • static void sort(char[] a, int fromIndex, int toIndex) - 将指定的数组范围以升序排序。
  • static void sort(double[] a) - 将指定的数组按升序数字排序。
  • static void sort(double[] a, int fromIndex, int toIndex) - 将数组的指定范围以升序排序。
  • static void sort(float[] a) - 将指定的数组以升序的数字排序。
  • static void sort(float[] a, int fromIndex, int toIndex) - 将指定的数组范围以升序排序。
  • static void sort(int[] a) - 将指定的数组以升序数字排序。
  • static void sort(int[] a, int fromIndex, int toIndex) - 将指定的数组范围以升序排序。
  • static void sort(long[] a) - 将指定的数组以升序数字排序。
  • static void sort(long[] a, int fromIndex, int toIndex) - 将指定的数组范围以升序排序。
  • static void sort(Object[] a) - 根据元素的自然排序,将指定的数组对象排序为升序。
  • static void sort(Object[] a, int fromIndex, int toIndex) - 根据元素的自然排序,将指定的对象数组的指定范围排序为升序。
  • static void sort(short[] a) - 将指定的数组排序为升序的数字顺序。
  • static void sort(short[] a, int fromIndex, int toIndex) - 将指定的数组范围以升序排序。
  • static void sort(T[] a, Comparator<? super T> c) - 根据指定的比较器引起的顺序对指定的数组对象进行排序。
  • static void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c) - 根据指定的比较器引起的顺序对指定的对象数组的指定范围排序。
    数组类提供了许多sort()重载方法,下面的例子展示了这些方法中的几个。

例子。

import java.util.Arrays;
import java.util.Date;
import java.util.List;

//*/*
/* java.util.Arrays Class API examples
/* 
/* @author javaguides.net
/*
/*/
public class ArraysJavaUtilClass {

        public static void main(String[] args) {
                // Sorts the specified array into ascending numerical order.
                int[] intArray = { 3, 1, 2, 4 };
                System.out.println("Original Array : " + Arrays.toString(intArray));
                Arrays.sort(intArray);
                System.out.println("Sorted Array : " + Arrays.toString(intArray));
                
                // Sorts the specified array into ascending numerical order.
                byte[] byteArray = { 1, 3, 2, 1, 5 };
                System.out.println("Original Array : " + Arrays.toString(byteArray));
                Arrays.sort(byteArray);
                System.out.println("Sorted Array : " + Arrays.toString(byteArray));

                // Sorts the specified array into ascending numerical order.
                char[] charArray = { 'a', 'd', 'c','b' };
                System.out.println("Original Array : " + Arrays.toString(charArray));
                Arrays.sort(charArray);
                System.out.println("Sorted Array : " + Arrays.toString(charArray));

                // Sorts the specified array into ascending numerical order.
                double[] doubleArray = { 0.1, 0.3, 0.2 };
                System.out.println("Original Array : " + Arrays.toString(doubleArray));
                Arrays.sort(doubleArray);
                System.out.println("Sorted Array : " + Arrays.toString(doubleArray));

                // Sorts the specified array into ascending numerical order.
                long[] longArray = { 1, 3, 2, 5, 4 };
                System.out.println("Original Array : " + Arrays.toString(longArray));
                Arrays.sort(longArray);
                System.out.println("Sorted Array : " + Arrays.toString(longArray));

                // Sorts the specified array into ascending numerical order.
                float[] floatArray = { 1.1f, 1.5f, 1.4f };
                System.out.println("Original Array : " + Arrays.toString(floatArray));
                Arrays.sort(floatArray);
                System.out.println("Sorted Array : " + Arrays.toString(floatArray));
        }
}

输出。

Original Array : [3, 1, 2, 4]
Sorted Array : [1, 2, 3, 4]
Original Array : [1, 3, 2, 1, 5]
Sorted Array : [1, 1, 2, 3, 5]
Original Array : [a, d, c, b]
Sorted Array : [a, b, c, d]
Original Array : [0.1, 0.3, 0.2]
Sorted Array : [0.1, 0.2, 0.3]
Original Array : [1, 3, 2, 5, 4]
Sorted Array : [1, 2, 3, 4, 5]
Original Array : [1.1, 1.5, 1.4]
Sorted Array : [1.1, 1.4, 1.5]

对字符串进行排序的例子

String内部实现了Comparable接口。

String[] strArray = { "abc", "cdf", "pqr" };
System.out.println("Original Array : " + Arrays.toString(strArray));
Arrays.sort(strArray);
System.out.println("Sorted Array : " + Arrays.toString(strArray));

输出:

Original Array : [abc, cdf, pqr]
Sorted Array : [abc, cdf, pqr]

对日期进行排序的例子

Date内部实现了Comparable接口。注意,LocalDate类来自Java 8。

LocalDate[] dates = { LocalDate.now(), LocalDate.of(2017, 12, 12) };
System.out.println("Original Array : " + Arrays.toString(dates));
Arrays.sort(dates);
System.out.println("Sorted Array : " + Arrays.toString(dates));

输出:

Original Array : [2018-08-13, 2017-12-12]
Sorted Array : [2017-12-12, 2018-08-13]

阵列的字符串表示法

  • static String toString(boolean[] a) - 返回指定数组内容的字符串表示。
  • static String toString(byte[] a) - 返回指定数组内容的字符串表示。
  • static String toString(char[] a) - 返回指定数组内容的字符串表示。
  • static String toString(double[] a) - 返回指定数组内容的字符串表示。
  • static String toString(float[] a) - 返回指定数组内容的字符串表示。
  • static String toString(int[] a) - 返回指定数组内容的字符串表示。
  • static String toString(long[] a) - 返回指定数组内容的字符串表示。
  • static String toString(Object[] a) - 返回指定数组内容的字符串表示。
  • static String toString(short[] a) - 返回指定数组内容的字符串表示。
//*/*
/* java.util.Arrays Class API examples
/* 
/* @author javaguides.net
/*
/*/
public class ArraysJavaUtilClass {

        public static void main(String[] args) {
                // Sorts the specified array into ascending numerical order.
                int[] intArray = { 3, 1, 2, 4 };
                System.out.println(" int Array toString : " + Arrays.toString(intArray));

                // Sorts the specified array into ascending numerical order.
                byte[] byteArray = { 1, 3, 2, 1, 5 };
                System.out.println(" byte Array toString : " + Arrays.toString(byteArray));

                // Sorts the specified array into ascending numerical order.
                char[] charArray = { 'a', 'd', 'c', 'b' };
                System.out.println(" char Array toString : " + Arrays.toString(charArray));

                // Sorts the specified array into ascending numerical order.
                double[] doubleArray = { 0.1, 0.3, 0.2 };
                System.out.println(" double Array toString : " + Arrays.toString(doubleArray));

                // Sorts the specified array into ascending numerical order.
                long[] longArray = { 1, 3, 2, 5, 4 };
                System.out.println(" long Array toString : " + Arrays.toString(longArray));

                // Sorts the specified array into ascending numerical order.
                float[] floatArray = { 1.1f, 1.5f, 1.4f };
                System.out.println(" float Array toString : " + Arrays.toString(floatArray));
                
                String[] strArray = { "abc", "cdf", "pqr" };
                System.out.println("string Array toString : " + Arrays.toString(strArray));

        }
}

输出

int Array toString : [3, 1, 2, 4]
 byte Array toString : [1, 3, 2, 1, 5]
 char Array toString : [a, d, c, b]
 double Array toString : [0.1, 0.3, 0.2]
 long Array toString : [1, 3, 2, 5, 4]
 float Array toString : [1.1, 1.5, 1.4]
 string Array toString : [abc, cdf, pqr]

相关文章