"=" is equivalent to passing a reference to an array variable to another array, and if an array changes, the variable referencing the same array will also change. Use a For loop to copy or copy each element of an array to the specified element, but the efficiency is almost using the Clone method to get the value of the array, rather than the reference, cannot copy the specified element, the flexibility is almost using system.arraycopy (SRC, srcpos, dest, Destpos, length) method, recommended to use, the most efficient
public static native void Arraycopy (object src, int srcpos, object dest, int destpos,int length);
SRC: source array; Srcpos: The starting position to copy the source array;
Dest: Destination array; Destpos: The starting position of the destination array placement;
Length: The size of the copy.
Because according to System.arraycopy () source, you can see is the native method: Native keyword description of its modified method is an original ecological method, the corresponding implementation of the method is not in the current file, but in other languages (such as C and C + +) implementation of the file. You can compare the native method to an interface between a Java program and a C program.
5. Copyof (), not the system method, but the arrays method, the following is the source code, can see essentially is the call arraycopy method. , then its efficiency is necessarily inferior to 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;
}
How to use:
1. Use clone
Int[] src={1,3,5,6,7,8};
Int[] dest;
Dest= (int[]) src.clone ();//Create a copy using clone, note that clone uses the cast
2. Use System.arraycopy
Int[] src={1,3,5,6,7,8};
int[] Dest = new Int[6];
System.arraycopy (src, 0, dest, 0, 6);