码迷,mamicode.com
首页 > 其他好文 > 详细

System.arraycopy和Arrays.copyOf的比较

时间:2015-08-17 23:19:26      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

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

System.arraycopy和Arrays.copyOf的比较

标签:

原文地址:http://www.cnblogs.com/pipi-style/p/4737979.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!