In Java, arrays can be duplicated with system.arraycopy and arrays.copyof (), and the differences between the two methods are described in detail below:
System.arraycopy
int[] src = {1,2,3,4,5}; int[] des = new INT[10]; System.arraycopy (arr, 0, copied, 1, 5); 5 is the length to copy System.out.println (arrays.tostring (DES));
Output Result:
[0, 1, 2, 3, 4, 5, 0, 0, 0, 0]
Arrays.copyof ()
Int[] des= arrays.copyof (SRC, 10); The length of the new ArraySystem.out.println (arrays.tostring (DES)); des= arrays.copyof (SRC, 3); System.out.println (arrays.tostring (DES));
Results:
[1, 2, 3, 4, 5, 0, 0, 0, 0, 0] [1, 2, 3]
Essential differences:
System.arraycopy () is more flexible, but requires a new number in advance
Arrays.copyof (), few parameters, flexible enough, can only specify the length of the copy, unable to set the offset
Java system.arraycopy () vs arrays.copyof ()