JAVA System.arraycopy 和Arrays.copyof 效率比较

System.arraycopy()源码。可以看到是native方法:

native关键字说明其修饰的方法是一个原生态方法,方法对应的实现不是在当前文件,而是在用其他语言(如C和C++)实现的文件中。 可以将native方法比作Java程序同C程序的接口。

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

copyOf,下面是源码,可以看到本质上是调用的arraycopy方法。,那么其效率必然是比不上 arraycopy的

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;
}

具体的理由不好说。不过一致的说法和实验证明是arraycopy()效率高!!!T...T

时间: 2024-08-02 09:16:26

JAVA System.arraycopy 和Arrays.copyof 效率比较的相关文章

java 之System.arraycopy() vs arrays.copyOf()

在java中,数组的复制可以有System.arraycopy与arrays.copyOf()两种选择,下面就详细介绍一下这两种方法的差别: System.arraycopy int[] src = {1,2,3,4,5}; int[] des = new int[10]; System.arraycopy(arr, 0, copied, 1, 5); //5 is the length to copy System.out.println(Arrays.toString(des)); 输出结果

Java中 System.arraycopy() 和 Arrays.copyOf()方法

System.arraycopy() 和 Arrays.copyOf()方法 阅读源码的话,我们就会发现 ArrayList 中大量调用了这两个方法.比如:我们上面讲的扩容操作以及add(int index, E element).toArray() 等方法中都用到了该方法! System.arraycopy() 方法 /** * 在此列表中的指定位置插入指定的元素. *先调用 rangeCheckForAdd 对index进行界限检查:然后调用 ensureCapacityInternal 方

Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别

如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 1.示例代码: System.arraycopy() int[] arr = {1,2,3,4,5}; int[] copied = new int[10]; System.arraycopy(arr, 0, copied, 1, 5);//5 is the length to copy System.out.print

java数组的拷贝四种方法:for、clone、System.arraycopy、arrays.copyof

public class ArrayCopy{ public static void main(String []args){ int []a = {1,3,4,5}; toPrint(a); int []aFor=new int[a.length]; //1.for循环复制 System.out.println("===========1.使用for复制"); for(int i=0;i<a.length;i++){ aFor[i]=a[i]; } aFor[2]=10;//改

System.arraycopy()和Arrays.copyOf()的区别

先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, int destPos,int length); src - 源数组. srcPos - 源数组中的起始位置. dest - 目标数组. destPos - 目标数据中的起始位置. length - 要复制的数组元素的数量. 该方法用了native关键字,说明调用的是其他语言写的底层函数. 再看Arra

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

System.arraycopy 和Arrays.copyOf

1.Arrays.copyOf()的实现是用的是arrayCopy(); 2.System.arrayCopy()需要目标数组,对两个数组的内容进行可能不完全的合并操作. 3.Arrays.copyOf()在内部新建一个数组,调用System.arrayCopy()将original内容复制到copy中去,并  且长度为newLength.返回copy; 所以,使用System.arrayCopy()必须确定原数组不为null,且新数组的容量必须大于原数组 Arrays.copyOf()则已经新

System.arraycopy() or Arrays.copyOf()

1. Simple Code Examples System.arraycopy() int[] arr = {1,2,3,4,5}; int[] copied = new int[10]; System.arraycopy(arr, 0, copied, 1, 5);//5 is the length to copy System.out.println(Arrays.toString(copied)); Output: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 1

Java数组的复制Arrays.copyOf()和System.arraycopy()函数

public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); arraycopy是个本地方法,无返回值. public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { T[] copy = ((Object)newType ==