System.arraycopy(src, srcPos, dest, destPos, length) 与 Arrays.copyOf(original, newLength)区别

//System.arraycopy,只拷贝已存在的数组元素
int[] src = {0, 1, 2};
int[] dest = new int[3];
System.arraycopy(src, 0, dest, 0, src.length);
System.out.println(Arrays.toString(dest)); //[0, 1, 2]
//Arrays.copyOf,会创建一个新的数组对象
int[] src = {0, 1, 2};
int[] dest = Arrays.copyOf(src, src.length);
System.out.println(Arrays.toString(dest)); //[0, 1, 2]

实际上Arrays.copyOf方法的内部实现也是通过System.arraycopy方法实现,在Arrays类中有多个copyOf的重载方法,现以拷贝int[]为例:

 public static int[] copyOf(int[] original, int newLength) {
int[] copy = new int[newLength];
System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
return copy;
上一篇:mysql 链接数满了的错误 ERROR 1040 (HY000): Too many connections


下一篇:Linux故障排查之磁盘IO、网络占用高