java 各种Array复制方式的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1697250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between various Array copy methods
提问by unj2
What is the difference between
之间有什么区别
System.arraycopy(),clone()- manual copying by iterating through the elements
Arrays.copyOf()- and just doing
arraynew = arrayold?
System.arraycopy(),clone()- 通过迭代元素手动复制
Arrays.copyOf()- 只是在做什么
arraynew = arrayold?
回答by Jo?o Silva
System.arraycopy()uses JNI (Java Native Interface) to copy an array (or parts of it), so it is blazingly fast, as you can confirm here;clone()creates a newarray with the same characteristics as the old array, i.e., same size, same type, and samecontents. Refer to herefor some examples ofclonein action;manual copyingis, well, manual copying. There isn't much to say about this method, except that many people have found it to be the most performant.arraynew = arrayolddoesn'tcopy the array; it just pointsarraynewto the memory address ofarrayoldor, in other words, you are simply assigning a referenceto the old array.
回答by Joachim Sauer
System.arraycopy()copies data from one existing array into another one and depending on the arguments only copies parts of it.clone()allocates a new array that has the same type and size than the original and ensures that it has the same content.- manual copying usually does pretty much the same thing than
System.arraycopy(), but is more code and therefore a bigger source for errors arraynew = arrayoldonly copies the reference to the array to a new variable and doesn't influence the array itself
System.arraycopy()将数据从一个现有数组复制到另一个数组中,并且根据参数仅复制其中的一部分。clone()分配一个与原始数组具有相同类型和大小的新数组,并确保它具有相同的内容。- 手动复制通常与 做几乎相同的事情
System.arraycopy(),但代码更多,因此是更大的错误来源 arraynew = arrayold只将数组的引用复制到一个新变量,不影响数组本身
There is one more useful option:
还有一个更有用的选项:
Arrays.copyOf()can be used to create a copy of another array with a different size. This means that the new array can be bigger or larger than the original array and the content of the common size will be that of the source. There's even a version that makes it possible to create an array of a different type, and a version where you can specify a range of elements to copy (Array.copyOfRange()).
Arrays.copyOf()可用于创建另一个具有不同大小的数组的副本。这意味着新数组可以比原始数组更大或更大,并且公共大小的内容将是源的内容。甚至还有一个版本可以创建不同类型的数组,还有一个版本可以指定要复制的元素范围 ( Array.copyOfRange())。
Note that all of those methods make shallow copies. That means that only the references stored in the arrays are copied and the referenced objects are notduplicated.
请注意,所有这些方法都会进行浅拷贝。这意味着只复制存储在数组中的引用,而不复制引用的对象。
回答by Venkata
Arrays.copyOf(..) uses System.arrayCopy(..) method internally.
Arrays.copyOf(..) 在内部使用 System.arrayCopy(..) 方法。
回答by Jeril Kuruvila
There are answers but not a complete one.
有答案,但不是完整的。
The options considered are
考虑的选项是
- Arrays.copyOf()
- System.arraycopy()
- Arrays.copyOf()
- System.arraycopy()
Below is the java implementation of Arrays.copyOf()
下面是 Arrays.copyOf() 的 java 实现
public static double[] More ...copyOf(double[] original, int newLength) {
double[] copy = new double[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
As you can see copyOf uses System.arraycopy internally.
如您所见,copyOf 在内部使用 System.arraycopy。
- If you already have an array created use System.arraycopy()to copy
- If you need the result in a new array use Arrays.copyOf()to copy
- 如果您已经创建了一个数组,请使用System.arraycopy()进行复制
- 如果您需要新数组中的结果,请使用Arrays.copyOf()进行复制
Note: There is no point in comparing the speed obviously because their functionalities differ.
注意:明显比较速度没有意义,因为它们的功能不同。

