1, of course, we are most familiar for circulation of
package test24;
import java.util.Arrays;
public class Array2 {
public static int[] copy1(int[] array) {
int[] array2 = new int[array.length];
System.out.println("拷贝前:"+Arrays.toString(array2));
for(int i = 0;i< array2.length;i++) {
array2[i] = array[i];
}
return array2;
}
public static void main(String[] args) {
int[] array = {
1,2,3,4,5};
int[] ret = copy1(array);
System.out.println("拷贝后:"+Arrays.toString(ret));
}
}
2. Call the copyOf (int[] original, int newLength) method
original: Indicates that the array is to be copied
newLength: Indicates the length of the new array
package test24;
import java.util.Arrays;
public class Array2 {
public static void main(String[] args) {
int[] array = {
1,2,3,4,5};
int[] ret = Arrays.copyOf(array,array.length);
System.out.println(Arrays.toString(ret));
}
}
3. Call arraycopy (Object var0, int var1, Object var2, int var3, int var4) method
var0: source array
var1: where in the source array to start copying
var2: copy to the destination array
var3: copy to where in the destination array
var4: How long copy
in this way are the four ways to copy copy fastest of
package test24;
import java.util.Arrays;
public class Array2 {
public static void main(String[] args) {
int[] array = {
1,2,3,4,5};
int[] ret = new int[array.length];
System.arraycopy(array,0,ret,0,array.length);
System.out.println(Arrays.toString(ret));
}
}
4. Cloning method: Generate a copy of the object to be cloned
package test24;
import java.util.Arrays;
public class Array2 {
public static void main(String[] args) {
int[] array = {
1,2,3,4,5};
int[] ret = array.clone();
System.out.println(Arrays.toString(ret));
}
}