Java数组的复制操作可以分为深复制和浅复制。一般对于引用类型的数组来说,需要在某些时候进行深复制。
首先来看一下两个方法实现的源代码,如下:
public staticT[] copyOf(T[] original, int newLength) { return (T[]) copyOf(original, newLength, original.getClass()); } public static T[] copyOf(U[] original, int newLength, Class newType) { T[] copy = ((Object)newType == (Object)Object[].class) (T[]) new Object[newLength] // 类型相同,则重新生成一个大小为newLength的数组实例 : (T[]) Array.newInstance(newType.getComponentType(), newLength); // 类型不同,重新生成一个大小为newLength的新类型数组实例 System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); //将原数组内容拷贝到新数组中,新数组取最小的数组长度 return copy; // 返回新数组的引用 }
而系统方法是一个本地方法,如下:
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);arraycopy 方法中,如果新数组比原数组的length小则会抛出java.lang.ArrayIndexOutOfBoundsException异常。
举个例子,如下:
public class test11 {
public static void main(String[] args) {
String[] strArray = new String[] { "ab", "xy", "mn" };
String[] copyArray = new String[2]; // 新数组大小比原数组小
for (int i = 0; i < strArray.length; i++) {
System.arraycopy(strArray, 0, copyArray, 0, strArray.length); // 调用系统方法进行深拷贝
}
printArray(copyArray);
System.out.println(strArray == copyArray "浅拷贝" : "深拷贝");
}
public static void printArray(String[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
}会抛出系统,将copyArray的大小改为4时,输出的结果如下:
ab xy mn null 深拷贝
使用copyOf()方法,如下:
public class test11 {
public static void main(String[] args) {
String[] strArray = new String[] { "ab", "xy", "mn" };
String[] copyArray=new String[4];
for(int i=0;i
如上输出:
ab xy mn 深拷贝
copyOf不会因为copyArray方法的大小而报错。因为copyOf 的返回值是在内部new 好的copy 数组,而该copy 数组new 的大小就等于newLength 。但是copyOf 方法最后也是调用System.arraycopy 的方法,不过由于之前已经对数组进行了处理,所以不会报出异常。