欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Arrays使用

程序员文章站 2022-03-11 22:04:31
...
public static void main(String[] args) {
        int arr[] = {1,3,5,4,6,2};

        printArr("before sort",arr);
        Arrays.sort(arr);
        printArr("after sort",arr);

        Arrays.fill(arr, 2);
        printArr("Arrays.fill(arr, 2)",arr);

        Arrays.fill(arr, 1,3,8);
        printArr("Arrays.fill(arr, 2)",arr);

        int []arr1 = {1,2,3};
        int []arr2 = {1,2,3};
        System.out.println("Arrays.equals(arr1,arr2)"+Arrays.equals(arr1,arr2));

        int []arr3 = {10,20,30,40,50};
        System.out.println("Arrays.binarySearch(arr3, 20)"+Arrays.binarySearch(arr3, 20));

        int []arr4 = {10,20,30,40,50};
        int []arr5 = Arrays.copyOf(arr4, 3);
        printArr("Arrays.copyOf(arr4, 3)",arr5);

        int []arr6 = Arrays.copyOfRange(arr4,1,3);
        printArr("Arrays.copyOfRange(arr4,1,3)",arr6);
    }

    public static void printArr(String str,int[] arr){
        System.out.print(str+" ");
        for(int i=0; i<arr.length; ++i){
            System.out.print(arr[i]+" ");
        }
        System.out.println("");
    }
/********************打印结果*******************
before sort 1 3 5 4 6 2 
after sort 1 2 3 4 5 6 
Arrays.fill(arr, 2) 2 2 2 2 2 2 
Arrays.fill(arr, 2) 2 8 8 2 2 2 
Arrays.equals(arr1,arr2)true
Arrays.binarySearch(arr3, 20)1
Arrays.copyOf(arr4, 3) 10 20 30 
Arrays.copyOfRange(arr4,1,3) 20 30
***********************************************/
相关标签: Arrays