【Java】Arrays.copyOf & System.arraycopy

public static T[] copyOf(T[] original, int newLength) 



    @SuppressWarnings("unchecked")
    public static  T[] copyOf(T[] original, int newLength) {
        return (T[]) copyOf(original, newLength, original.getClass());
    

    
    public static  T[] copyOf(U[] original, int newLength, Class 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,除此之外,多了一步创建对象(新集合)的过程

 

public static native void arraycopy

           (Object src,  int  srcPos, Object dest, int destPos, int length);


    public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);

*用于将旧数组的数据复制到新数组

src:原数组(注意这里是将数组当成Object对象)

srcPos:原数组的起始位置

dest:目标数组

destPos:目标数组的起始位置

length:要复制的元素个数

你可能感兴趣的:(JAVA,数组)