# Arrays类复制数组方法解析

# Arrays类的定义

public class Arrays {

	// 抑制默认构造函数,确保不可实例化。
    // Suppresses default constructor, ensuring non-instantiability.
    private Arrays() {}

}
1
2
3
4
5
6
7

# 静态copyOf方法

被调用的地方:

    elementData = Arrays.copyOf(elementData, newCapacity);
1
    
		// 第一步
	public static <T> T[] copyOf(T[] original, int newLength) {
        return (T[]) copyOf(original, newLength, original.getClass());
    }
	
	// 第二步
    public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        @SuppressWarnings("unchecked")
        T[] copy = ((Object)newType == (Object)Object[].class)
            // Object类型数组
            ? (T[]) new Object[newLength]
            // 其它类型数组。false
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
        
        // 复制数组
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

第二步的参数:

U[] original:原来数组

int newLength:新长度

Class<? extends T[]> newType:原来数组类型字节码。可以是 T[] 或任何 S[](S 是 T 的子类

# native复制数组的方法

// 参数1:original 原来数组
// 参数2:原来数组开始位置为 0 
// 参数3:目标新数组
// 参数4:目标新数组开始位置为 0 
// 参数5:Math.min(original.length, newLength)复制多少元素?一般的原来数组的长度。
System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
1
2
3
4
5
6
7

Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest. The number of components copied is equal to the length argument. The components at positions srcPos through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, respectively, of the destination array. If the src and dest arguments refer to the same array object, then the copying is performed as if the components at positions srcPos through srcPos+length-1 were first copied to a temporary array with length components and then the contents of the temporary array were copied into positions destPos through destPos+length-1 of the destination array. If dest is null, then a NullPointerException is thrown. If src is null, then a NullPointerException is thrown and the destination array is not modified. Otherwise, if any of the following is true, an ArrayStoreException is thrown and the destination is not modified: The src argument refers to an object that is not an array. The dest argument refers to an object that is not an array. The src argument and dest argument refer to arrays whose component types are different primitive types. The src argument refers to an array with a primitive component type and the dest argument refers to an array with a reference component type. The src argument refers to an array with a reference component type and the dest argument refers to an array with a primitive component type. Otherwise, if any of the following is true, an IndexOutOfBoundsException is thrown and the destination is not modified: The srcPos argument is negative. The destPos argument is negative. The length argument is negative. srcPos+length is greater than src. length, the length of the source array. destPos+length is greater than dest. length, the length of the destination array. Otherwise, if any actual component of the source array from position srcPos through srcPos+length-1 cannot be converted to the component type of the destination array by assignment conversion, an ArrayStoreException is thrown. In this case, let k be the smallest nonnegative integer less than length such that src[srcPos+k] cannot be converted to the component type of the destination array; when the exception is thrown, source array components from positions srcPos through srcPos+k-1 will already have been copied to destination array positions destPos through destPos+k-1 and no other positions of the destination array will have been modified. (Because of the restrictions already itemized, this paragraph effectively applies only to the situation where both arrays have component types that are reference types.) Params: src – the source array. srcPos – starting position in the source array. dest – the destination array. destPos – starting position in the destination data. length – the number of array elements to be copied. Throws: IndexOutOfBoundsException – if copying would cause access of data outside array bounds. ArrayStoreException – if an element in the src array could not be stored into the dest array because of a type mismatch. NullPointerException – if either src or dest is null.

package java.lang;

public final class System {    

public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);
    
}
1
2
3
4
5
6
7
8
9

将指定源数组中的数组从指定位置开始复制到目标数组的指定位置。数组元素的一个子序列从src引用的源数组复制到dest引用的目标数组。复制的元素数量等于length参数。源数组中位置srcPossrcPos+length-1的元素分别被复制到目标数组的位置destPosdestPos+length-1

如果srcdest参数引用同一个数组对象,则复制操作执行时,先将位置srcPossrcPos+length-1的元素复制到一个具有length个元素的临时数组,然后将临时数组的内容复制到目标数组的位置destPosdestPos+length-1

如果destnull,则抛出NullPointerException。 如果srcnull,则抛出NullPointerException且目标数组不会被修改。

否则,如果以下任何一项为真,则抛出ArrayStoreException且目标数组不会被修改:

  • src参数引用的对象不是数组。
  • dest参数引用的对象不是数组。
  • src参数和dest参数引用的数组具有不同的基本数据类型元素类型。
  • src参数引用具有基本数据类型元素类型的数组,而dest参数引用具有引用类型元素类型的数组。
  • src参数引用具有引用类型元素类型的数组,而dest参数引用具有基本数据类型元素类型的数组。

否则,如果以下任何一项为真,则抛出IndexOutOfBoundsException且目标数组不会被修改:

  • srcPos参数为负数。
  • destPos参数为负数。
  • length参数为负数。
  • srcPos+length大于源数组的长度src.length
  • destPos+length大于目标数组的长度dest.length

否则,如果源数组中从位置srcPossrcPos+length-1的任何实际元素无法通过赋值转换转换为目标数组的元素类型,则抛出ArrayStoreException。在这种情况下,设k为小于length的最小非负整数,使得src[srcPos+k]无法转换为目标数组的元素类型;当抛出异常时,源数组中位置srcPossrcPos+k-1的元素将已经被复制到目标数组位置destPosdestPos+k-1,且目标数组的其他位置不会被修改。(由于已经列出的限制,本段实际上仅适用于两个数组都具有引用类型元素类型的情况。)

参数:

  • src - 源数组。
  • srcPos - 源数组中的起始位置。
  • dest - 目标数组。
  • destPos - 目标数据中的起始位置。
  • length - 要复制的数组元素数量。

抛出:

  • IndexOutOfBoundsException - 如果复制将导致访问数组边界外的数据。
  • ArrayStoreException - 如果由于类型不匹配,src数组中的元素无法存储到dest数组中。
  • NullPointerException - 如果srcdestnull
Last Updated: 4/3/2026, 6:47:37 AM