Java提供了对数组的快速克隆,Arrays.copyOf()和System.arraycopy()。
环境
- eclipse 2019-03 (4.11.0)
- jdk-12.0.1
一、System.arraycopy()
System.arraycopy()有五个参数:
- 第一个参数src:要复制的数组
- 第二个参数srcPos:要复制的数组中的起始位置
- 第三个参数dest:副本数组。预先创建好的数组
- 第五个参数destPos:副本数组中的起始位置
- 第六个参数length:要复制的数组元素的数量
列:
import java.util.Arrays;
public class Run {
public static void main(String[] args) {
int[] datas=new int[] {3,4,5,6,7,8,9};
int[] copy=new int[9];
copy[0]=1;
copy[1]=2;
System.arraycopy(datas, 0, copy, 2, datas.length);
System.out.println(Arrays.toString(copy));//optput:[1,2,3,4,5,6,7,8,9]
}
}
源码
@HotSpotIntrinsicCandidate
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
源码分析:使用了native关键字,表明为本地方法,调用了C++编写的底层函数
二、Arrays.copyOf()
如图所示,Arrays.copyOf()有十个重载方法,每个基本类型数组都有对应到。此外还包括了复杂类型数组(对象数组)的重载方法。每个方法都将返回一个新数组。
- 第一个参数original:要复制的数组。
- 第二个参数newLength:要返回的副本的长度。若该值比要复制的数组original的长度更大,则将多余的结果设置为0或false
- 第三个参数newType:要返回的副本的类型。如果没有指定该参数,则返回的副本的类型与original的类型相同
列:
import java.util.Arrays;
public class Run {
public static void main(String[] args) {
int[] datas=new int[] {1,2,3,4,5,6,7,8,9};
int[] copyDatas=Arrays.copyOf(datas, datas.length);
Run[] runs=new Run[10];
Object[] copyRuns=Arrays.copyOf(runs, 10, Object[].class);
}
}
来看看Arrays.copyOf的源码
//复杂类型数组
@SuppressWarnings("unchecked")
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}
@HotSpotIntrinsicCandidate
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
//基本数据类型,只列举了两个,其他的基本代码不变
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;
}
public static boolean[] copyOf(boolean[] original, int newLength) {
boolean[] copy = new boolean[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
/*...*/
源码分析:查看源码就可以知道
Arrays.copyOf的内部实现其实是依赖于System.arraycopy的。
方法返回的副本都是一个新数组
创建副本数组时,其长度选择参数值与原数组长度中的最小值
我们来看看复杂类型数组复制方法copyOf(U[] original, int newLength, Class<? extends T[]> newType)中创建新数组对象的实现。
@SuppressWarnings("unchecked") T[] copy = ((Object)newType == (Object)Object[].class) ? (T[]) new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(), newLength);
此段代码首先比较第三个参数newType是否为Object数组类型,若是则采用new关键字创建对象;若不是则调用Array.newInstance方法动态创建数组对象(反射)。
总结
Arrays.copyOf()会返回一个新数组。该新数组在方法内部被创建
System.arraycopy()需要预先创建好一个目标数组,且不会返回任何值。比如当希望数据的操作在原数组中进行时,只需将原数组作为目标数组即可。
Arrays.copyOf()实际上是调用了System.arraycopy()
关于Arrays.copyOf()与System.arraycopy()的实例可以查看ArrayList源码。源码中很清晰的表达了什么时候该用Arrays.copyOf(),而什么时候该用System.arraycopy()