数组的拷贝有四种常用的方法
对于一位数组来说都是浅拷贝,如果是多维数组,需要进行递推拷贝或者通过反序列化,相关序列化克隆内容在
对象的克隆;
clone
该类是通过object的clone方法进行拷贝数组的
public class CopyArray {
public static void main(String[] args) {
int[] intArray = {1,2,3,4,5,6,7,8,9};
int[] cloneArray = intArray.clone(); 使用clone方法进行克隆;
}
}
这里为什么可以使用clone()方法呢,难道数组类实现了cloneable接口?
1、Java中并不存在任何一个类对应数组,数组属于Java语言的一部分。
2、数据是特殊的对象,本身就实现了Cloneable。Object的clone方法的javadoc中有这么一句Note that all arrays are considered to implement the interface Cloneable ,所以数组是可以直接使用clone方法的。
3、数组对象天生就有一个final的length属性,因为数组并没有定义在任何一个类中,所以没有源码。
System.arraycopy
public class CopyArray {
public static void main(String[] args) {
int[] sysArrayCopy = new int[intArray.length];、
//arraycopy是操作系统的arraycopy方法~
System.arraycopy(intArray, 0, sysArrayCopy, 0, intArray.length);
}
}
Arrays.copyOf
public class CopyArray {
public static void main(String[] args) {
//Arrays类封装的copyOf方法,底层原理依然是System.arraycopy()类
int[] arrayCopyOf = Arrays.copyOf(intArray, intArray.length);
}
}
底层源码
/**
* Copies the specified array, truncating or padding with zeros (if necessary)
* so the copy has the specified length. For all indices that are
* valid in both the original array and the copy, the two arrays will
* contain identical values. For any indices that are valid in the
* copy but not the original, the copy will contain <tt>0</tt>.
* Such indices will exist if and only if the specified length
* is greater than that of the original array.
*
* @param original the array to be copied
* @param newLength the length of the copy to be returned
* @return a copy of the original array, truncated or padded with zeros
* to obtain the specified length
* @throws NegativeArraySizeException if <tt>newLength</tt> is negative
* @throws NullPointerException if <tt>original</tt> is null
* @since 1.6
*/
public static int[] copyOf(int[] original, int newLength) {
int[] copy = new int[newLength]; //提前设置了数组,之后System.arraycopy(),将之返回;
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
Arrays.copyOfRange
public class CopyArray {
public static void main(String[] args) {
//Arrays类封装的copyOfRange方法,底层原理依然是System.arraycopy()类
int[] copyOfRange = Arrays.copyOfRange(intArray, 0, intArray.length);
}
}
底层源码
/**
* Copies the specified range of the specified array into a new array.
* The initial index of the range (<tt>from</tt>) must lie between zero
* and <tt>original.length</tt>, inclusive. The value at
* <tt>original[from]</tt> is placed into the initial element of the copy
* (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
* Values from subsequent elements in the original array are placed into
* subsequent elements in the copy. The final index of the range
* (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
* may be greater than <tt>original.length</tt>, in which case
* <tt>0</tt> is placed in all elements of the copy whose index is
* greater than or equal to <tt>original.length - from</tt>. The length
* of the returned array will be <tt>to - from</tt>.
*
* @param original the array from which a range is to be copied
* @param from the initial index of the range to be copied, inclusive
* @param to the final index of the range to be copied, exclusive.
* (This index may lie outside the array.)
* @return a new array containing the specified range from the original array,
* truncated or padded with zeros to obtain the required length
* @throws ArrayIndexOutOfBoundsException if {@code from < 0}
* or {@code from > original.length}
* @throws IllegalArgumentException if <tt>from > to</tt>
* @throws NullPointerException if <tt>original</tt> is null
* @since 1.6
*/
public static int[] copyOfRange(int[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
int[] copy = new int[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}