Arrays.copyOf()用于复制数组内容,可实现扩容。该方法对不同数据类型有相应的重载方法,详见源码。
copyOf(T[] original, int newLength):
/*** Copies the specified array, truncating or padding with nulls (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>null</tt>.* Such indices will exist if and only if the specified length* is greater than that of the original array.* The resulting array is of exactly the same class as the original array.** @param <T> the class of the objects in the 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 nulls* to obtain the specified length* @throws NegativeArraySizeException if <tt>newLength</tt> is negative* @throws NullPointerException if <tt>original</tt> is null* @since 1.6*/@SuppressWarnings("unchecked")public static <T> T[] copyOf(T[] original, int newLength) {return (T[]) copyOf(original, newLength, original.getClass());}
original为要复制的数组,newLength为要复制的长度,默认从0开始复制。复制长度大于原数组长度,使用默认值填充,反之复制到第length个元素(length-1)即可。
T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType):
/*** Copies the specified array, truncating or padding with nulls (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>null</tt>.* Such indices will exist if and only if the specified length* is greater than that of the original array.* The resulting array is of the class <tt>newType</tt>.** @param <U> the class of the objects in the original array* @param <T> the class of the objects in the returned array* @param original the array to be copied* @param newLength the length of the copy to be returned* @param newType the class of the copy to be returned* @return a copy of the original array, truncated or padded with nulls* to obtain the specified length* @throws NegativeArraySizeException if <tt>newLength</tt> is negative* @throws NullPointerException if <tt>original</tt> is null* @throws ArrayStoreException if an element copied from* <tt>original</tt> is not of a runtime type that can be stored in* an array of class <tt>newType</tt>* @since 1.6*/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;}
代码拷贝调用的是System.arraycopy()方法,返回新数组对象,不会影响原数组。
System.arraycopy():
public static native void arraycopy(Object src, int srcPos,Object dest, int destPos,int length);
src:源对象
srcPos:源数组中的起始位置
dest:目标数组对象
destPos:目标数据中的起始位置
length:要拷贝的数组元素的数量
Java实现数组复制的4种方法:
1.Arrays.copyOf()
2.Arrays.copyOfRange()
3.System.arraycopy()
4.Object.clone()
Arrays.copyOfRange():
/*** 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>null</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>.* <p>* The resulting array is of exactly the same class as the original array.** @param <T> the class of the objects in the array* @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 nulls 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*/@SuppressWarnings("unchecked")public static <T> T[] copyOfRange(T[] original, int from, int to) {return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass());}/*** 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>null</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>.* The resulting array is of the class <tt>newType</tt>.** @param <U> the class of the objects in the original array* @param <T> the class of the objects in the returned array* @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.)* @param newType the class of the copy to be returned* @return a new array containing the specified range from the original array,* truncated or padded with nulls 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* @throws ArrayStoreException if an element copied from* <tt>original</tt> is not of a runtime type that can be stored in* an array of class <tt>newType</tt>.* @since 1.6*/public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {int newLength = to - from;if (newLength < 0)throw new IllegalArgumentException(from + " > " + to);@SuppressWarnings("unchecked")T[] copy = ((Object)newType == (Object)Object[].class)? (T[]) new Object[newLength]: (T[]) Array.newInstance(newType.getComponentType(), newLength);System.arraycopy(original, from, copy, 0,Math.min(original.length - from, newLength));return copy;}
original表示原数组。
from表示开始复制的起始索引,目标数组中将包含起始索引对应的元素,另外,from必须在 0 到 original.length 之间。
to表示终止索引,目标数组中将不包含终止索引对应的元素,to必须大于等于 from,可以大于 original.length,如果大于 original.length,则目标数组中使用默认值填充。
Object.clone():
int[] targetArray=(int[])sourceArray.clone();
以上方法都是浅拷贝(浅复制)。浅拷贝只是复制了对象的引用地址,两个对象指向同一个内存地址,所以修改其中任意的值,另一个值都会随之变化。深拷贝是将对象及值复制过来,两个对象修改其中任意的值另一个值不会改变。
