The practical function of Arrays class -- JAVA programming thought 65

Today, I'd like to share with you the relevant practical functions of the and Arrays class.

1. Print array

Arrays.toString() can represent arrays as String types.

public class ArrayToString {

    public static void main(String[] args) {
        String[] strings = {"a", "b", "c"};
        System.out.println(Arrays.toString(strings));
        int[] ints = {1, 2, 3};
        System.out.println(Arrays.toString(ints));
    }

}
[a, b, c]
[1, 2, 3]

2. Fill array

When creating an array, the basic type will be given a default value, and the non basic type will be given null. You can fill the entire array with Arrays.fill(). Unfortunately, fill () only allows the entire array to be filled with the same value.

public class FillArray {

    public static void main(String[] args) {
        int[] ints = new int[5];
        Arrays.fill(ints, 2);
        System.out.println(Arrays.toString(ints));
        boolean[] booleans = new boolean[6];
        Arrays.fill(booleans, true);
        System.out.println(Arrays.toString(booleans));
        String[] strings = new String[7];
        Arrays.fill(strings,"A");
        System.out.println(Arrays.toString(strings));
    }

}
[2, 2, 2, 2, 2]
[true, true, true, true, true, true]
[A, A, A, A, A, A, A]

3. Copy array

Here is an additional method that does not belong to the Arrays class, but is very practical. System.arraycopy() can copy the elements of one array into another array. It requires five parameters: source array, subscript at the beginning of source array, target array, subscript at the beginning of target array, and the number of elements to be copied from source array.

public class CopyArray {

    public static void main(String[] args) {
        int[] a = new int[5];
        Arrays.fill(a, 12);
        System.out.println(Arrays.toString(a));
        int[] b = new int[7];
        Arrays.fill(b, 15);
        System.out.println(Arrays.toString(b));
        System.arraycopy(a, 0, b, 0, a.length);
        System.out.println(Arrays.toString(b));
    }

}
[12, 12, 12, 12, 12]
[15, 15, 15, 15, 15, 15, 15]
[12, 12, 12, 12, 12, 15, 15]

4. Array comparison

Arrays.equals() can be used to compare whether the elements contained in the two arrays are equal. It can be judged by calling the equals() method of each element (the basic type calls the equals() method of the wrapper type). If the elements in the same position are equal, true is returned. If the elements in any position are not equal, false is returned. The lengths of the two comparison arrays must be equal.

public class CompareArray {

    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        int[] b = {1, 2, 3};
        System.out.println(Arrays.equals(a, b));
        String[] c = {"a", "b", "c"};
        String[] d = {"a", "b", "d"};
        System.out.println(Arrays.equals(c, d));
    }

}

true
false

5. Array sorting

You can sort the elements in the array through Arrays.sort(). The default is ascending according to the size of the elements.

public class SortArray {

    public static void main(String[] args) {
        int[] ints = {12, 94, 75, 24, 14, 17, 9, 64, 32, 21, 30, 54, 27};
        Arrays.sort(ints);
        System.out.println(Arrays.toString(ints));
    }

}
[9, 12, 14, 17, 21, 24, 27, 30, 32, 54, 64, 75, 94]

If reverse sorting is required, the basic type cannot be used, and its wrapper class must be used, as shown below:

public class SortArray {

    public static void main(String[] args) {
        Integer[] ints = {12, 94, 75, 24, 14, 17, 9, 64, 32, 21, 30, 54, 27};
        Arrays.sort(ints, Collections.reverseOrder());
        System.out.println(Arrays.toString(ints));
    }

}
[94, 75, 64, 54, 32, 30, 27, 24, 21, 17, 14, 12, 9]

This sharing is over. I hope this article will help you. If you can light the like button below, I'd be grateful. Thank you for your [spiritual support].

If you have any questions, you are also welcome to communicate with me. If there are deficiencies, you are also welcome to correct!

Keywords: Java Algorithm array

Added by abushahin on Sat, 30 Oct 2021 21:17:27 +0300