Difference between various Array copy methods
What is the difference between
System.arrayc开发者_JAVA技巧opy()
,clone()
- manual copying by iterating through the elements
Arrays.copyOf()
- and just doing
arraynew = arrayold
?
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 new array with the same characteristics as the old array, i.e., same size, same type, and same contents. Refer to here for some examples ofclone
in action;manual copying
is, 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 = arrayold
doesn't copy the array; it just pointsarraynew
to the memory address ofarrayold
or, in other words, you are simply assigning a reference to the old array.
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 = arrayold
only copies the reference to the array to a new variable and doesn't influence the array itself
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()
).
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 not duplicated.
Arrays.copyOf(..) uses System.arrayCopy(..) method internally.
There are answers but not a complete one.
The options considered are
- Arrays.copyOf()
- System.arraycopy()
Below is the java implementation of Arrays.copyOf()
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.
- 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
Note: There is no point in comparing the speed obviously because their functionalities differ.
- System.arraycopy() : Used to copy a part of array from mentioned source array into destination array on specified indexes. System.arraycopy() is faster than clone() as it uses Java Native Interface.
public static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length)
This method takes the following arguments:
-a source array,
-the starting position to copy from the source array,
-a destination array,
-the starting position in the destination array, and
-the number of elements to be copied.
Example:
int[] array = {0,1,2,3,4,5,6,7,8};
int[] copiedArray = new int[3];
System.arraycopy(array, 2, copiedArray, 0, 3);
copiedArray will be [2,3,4]
Explanation: Starting from source array index 2 it will copy into the destination array from index 0 to length 3. So basically, you can copy a part of array into another one.
- Object.clone()
int[] array = {2, 4};
int[] copiedArray = array.clone();
In the above example, they will have the same content after cloning, but they hold different references, so any change in one of them won't affect the other one.
- Arrays.copyOf(): uses the same approach provided by the System class that we previously examined.
int[] array = {2, 4, 5, 2};
int newLength = array.length;
int[] copiedArray = Arrays.copyOf(array, newLength);
- arr1 = arr2: Here simply reference being assigned not the actual copying of each item. So any change in any of them will be refected in the other.
Arrays.copyOf() offers additional functionality on top of what System.arraycopy() offers. While System.arraycopy() simply copies values from the source array to the destination, Arrays.copyOf() also creates new array. If required, it will truncate or pad the content.
The second difference is that the new array can be of a different type than the source array. In this case, the JVM will use reflection, which adds performance overhead.
精彩评论