Arrays.coyOf()和System.arraycopy()的区别

Arrays.coyOf()

public class Main {

    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 4, 5};
        //这个方法含义:arr1复制到arr2 长度为8
        int[] arr2 = Arrays.copyOf(arr1, 8);
        System.out.println(Arrays.toString(arr2));

    }
}
//输出结果:[1, 2, 3, 4, 5, 0, 0, 0]

System.arraycopy()


public class Main {

    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 4, 5};
        //这个方法的含义:从arr1数组索引3开始复制到2位到arr1从索引2
        System.arraycopy(arr1,3,arr1,2,2);  
        System.out.println(Arrays.toString(arr1));

    }
}

你可能感兴趣的:(java基础)