Java集合(4)--System.arraycopy和Arrays.copyOf的比较

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

用了native关键字,调用的为C++编写的底层函数,可见其为JDK中的底层函数。

public static int[] copyOf(int[] original, int newLength) {  
        int[] copy = new int[newLength];  
        System.arraycopy(original, 0, copy, 0,  
                         Math.min(original.length, newLength));  
        return copy;  
    } 

 

Arrays.copyOf调用了System.arraycopy,返回一个新的数组copy,但是copy中的有效长度是Math.min(original.length, newLength),容量是newLength

posted @ 2015-08-17 22:20  山野豪杰  阅读(219)  评论(0编辑  收藏  举报