The array returned by this method is a new array object. Changing the element values in the returned array will not affect the original array. The first variable represents the original array object , and the second variable represents the length of the new array . If the length of the new array exceeds the length of the original array, the default basic type values of the array elements are retained.
For example:
importjava.util.Arrays;publicclassMain{publicstaticvoidmain(String[]args){int[]arr1={1,2,3,4,5};int[]arr2=Arrays.copyOf(arr1,4);int[] arr3=Arrays.co pyOf(arr1,8);for(inti=0;i<arr2.length;i++)System.out.print(arr2[i]+);System.out.println();for(inti=0;i< arr3.length;i++)System.out.print(arr3[i]+);}}
The running results are as follows:
123412345000