Java foundation - Arrays tool class

Arrays is a tool class for manipulating arrays provided by JDK. Arrays class provides methods for dynamically creating, accessing and manipulating Java arrays. This class also contains a static factory that allows an array to be viewed as a list.

1. Convert array to string

public static String toString(T [] a)

For example:

int[] a = {2,8,51,13,46,11,22};

2. Convert array to collection


public static <T> List<T> asList(T... a)


List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

1) This method is applicable to arrays of object-oriented data (String, Integer...). This method should not be used for arrays of basic data types (byte,short,int,long,float,double,boolean);
2) This method links the array with the List: when one of them is updated, the other is automatically updated;
3) Methods such as add(), remove(), clear() are not supported;
4) The length of the List obtained by this method cannot be changed;
5) If your List is only used for traversal, use Arrays.asList(); If you want to add or delete elements in your List, create a java.util.ArrayList, and then add or delete elements one by one;
6) This ArrayList is not under the java.util package, but java.util.Arrays.ArrayList. It is a static inner class defined by the Arrays class itself. This inner class does not implement the add() and remove() methods, but directly uses the corresponding methods of its parent class AbstractList.

(2) stream operation

public static IntStream stream(int[] array)


int[] a = {2,8,51,13,46,11,22};
IntStream stream =;

Turn the array into streaming and stream the array. All streaming methods can be used.


3. Sort the array in ascending order

public static void sort(T [] a)


int[] a = {20, 3, 32, 1, 72, 26, 35};

4. Determine whether the arrays are equal

public static boolean equals(T[] a, T[] a2)


int[] a = {20, 3, 32, 1, 72, 26, 35};
int[] b = {3, 5, 7, 8, 54, 23, 9};
boolean boo = Arrays.equals(a, b);

The principle of comparison is equal length and equal elements.

5. Assign specific values to all elements

// Replace the value of the specified range of the array with val
public static void fill(T[] a, int fromIndex, int toIndex, T val)


int[] a = {1, 2, 3, 4};
Arrays.fill(a, 0,2,5);

6. Copy to array

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


int[] b = {3, 5, 7, 8, 54, 23, 9};
int[] d = Arrays.copyOf(b, b.length);

The array returned by the copyOf() method of Arrays is a new array object. Changing the element value in the returned array will not affect the original array.
The second argument of copyOf() specifies the length of the new array to be created. If the length of the new array exceeds the length of the original array, the default value of the array will be retained.

7. Query array subscript

public static int binarySearch(byte[] a, byte key)     // Query where the element first appears


int[] b = {3, 5, 7, 8, 9, 23, 54};
int i = Arrays.binarySearch(b, 5);

Use the binary search method to search the specified array, and the method returns the index value of the element to be searched.
Note: you must sort the array (sort method) before making this call. If the array is not sorted, the result is ambiguous. If the array contains more than one element with the specified value, there is no guarantee which one is found.
The search efficiency is faster than the average search time of the general search from left to right in the array.

8. Returns an array hashcode value

public static int hashCode(int a[]);    // Returns the hashCode value of the array


int[] a = {1, 2, 3, 4};
// Result: 955331

Tags: Java

Posted on Fri, 03 Sep 2021 13:36:56 -0400 by astropirate