Arrays common method of presentation tools

java.util.Arrays is operated tools JDK array, comprising a variety of methods for manipulating arrays (such as sorting and searching).

Here we have an int array, for example, learning under the common method, other types of arrays are similar.

1.equals (int [] a, int [] b) Method: determining whether the two arrays are equal

    int[] array1 = new int[]{1, 2, 3, 4};
    int[] array2 = new int[]{1, 2, 3, 4};
    int[] array3 = new int[]{1, 3, 2, 4};
    boolean b1 = Arrays.equals(array1, array2);
    boolean b2 = Arrays.equals(array1, array3);
    System.out.println(b1);// 返回true
    System.out.println(b2);// 返回false
复制代码

2.toString (int [] a) Method: Returns a string representation of the specified array

    int[] array1 = new int[]{1, 2, 3, 4};
    System.out.println(Arrays.toString(array1));
	// 输出结果为[1, 2, 3, 4]
复制代码

3.fill (int [] a, int value) method: the value assigned to each element of the specified array designated

    int[] array1 = new int[5];
    Arrays.fill(array1, 1);
    System.out.println(Arrays.toString(array1));
	// 输出结果为[1, 1, 1, 1, 1]
复制代码

4.sort (int [] a): ascending sort specified array

    int[] array = new int[]{99, 23, 33, 0, 65, 9, 16, 84};
    Arrays.sort(array);
    System.out.println(Arrays.toString(array));
	// 输出结果为[0, 9, 16, 23, 33, 65, 84, 99]
复制代码

5.binarySearch (int [] a, int value): using the binary search algorithm searches the specified value in a specified array and returns the index where the value position; if not query -1

    int[] array = new int[]{1, 17, 20, 44, 45, 62, 79, 88, 93};
    int i = Arrays.binarySearch(array, 44);
    System.out.println(i);
	// 输出结果为3
复制代码

Guess you like

Origin juejin.im/post/5d20763e5188250fcf17bf6e