JAVA commonly used Arrays array tool class

By using these tools, we can simplify our code, optimize operating efficiency, and avoid unnecessary redundancy. But before that, we still have to understand the specific implementation methods of the tool class, and then look at these to facilitate our own understanding.

boolean equals(int[] a,int[] b); Determine whether two arrays are equal

int[] arr1 = new int[]{
    
    1, 2, 3, 4};
        int[] arr2 = new int[]{
    
    1, 3, 2, 4};
        boolean isEquals = Arrays.equals(arr1, arr2);
        System.out.println(isEquals);

String toString(int[] a); Output the information of the array (traversal)

System.out.println(Arrays.toString(arr1));

void fill(int[] a,int val); fill the specified value into the array (all) Val is the number to be filled

Arrays.fill(arr1, 10);
        System.out.println(Arrays.toString(arr1));

void sort(int[] a); sort the array

Arrays.sort(arr2);
        System.out.println(Arrays.toString(arr2));

int binarySearch(int[] a,int key); Key is the value to be searched.

int[] arr3 = new int[]{
    
    -98, -34, 2, 34, 54, 66, 79, 105, 210, 333};
        int index = Arrays.binarySearch(arr3, 54);
        if (index >= 0){
    
    
            System.out.println(index);
        }else {
    
    
            System.out.println("未找到!");
        }

Guess you like

Origin blog.csdn.net/qq_30068165/article/details/111998304