Copy the content of an array (source) to another array (destination) in Java
Description
The following code shows how to copy the content of an array (source) to another array (destination).
System.arraycopy()
copies the content of an array (source) to another array (destination),
beginning at the specified position, to the specified position
of the destination array.
Example
public class Main {
/*from www .j a va 2s . c o m*/
public static void main(String[] a) {
int[] array1 = { 1, 2, 3, 4 };
int[] array2 = new int[array1.length];
System.arraycopy(array1, 0, array2, 0, array1.length);
for(int i: array2){
System.out.println(i);
}
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »