Copy of array - difference
What is the difference between these two ways of array copying and which way is better?
Code:
int[] array1 = new int[7];
int[] array2;
array2 = array1.clone();
Code:
int[] array1 = new int[7];
int[] array2 = new int[array1.length];
System.arraycopy(array1, 0, array2, 0, array1.length);
Re: Copy of array - difference
For one, System.arraycopy has been optimized for copying arrays and is significantly faster at copying items from one array to another. AFAIK, neither does a deep copy though.
Re: Copy of array - difference
Also have a look at the Arrays utility class; it has a bunch of copyOf( ... ) methods; I bet they are optimized for the job.
kind regards,
Jos
Re: Copy of array - difference
Can I say: Arrays class is specialized for dealing with reference values of many array types?