Copy an array
- Write code to implement ArrayCopy (content copy) arraycopy.java
- will
Int [] arr1 = {10, 30};
Copy toarr2
Arrays require that the data space be independent.
int[] arr1 = {10.20.30};
// Create a new array arr2 to create a new data space
/ / size arr1. Length;
int[] arr2 = new int[arr1.length];
// Iterate over arr1, copying each element to its arR2 location
for(int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
// Modify ARR2, arR1 will not be affected.
arr2[0] = 100;
/ / output arr1
System.out.println("=== elements of ARr1 ===");
for(int i = 0; i < arr1.length; i++) {
System.out.println(arr1[i]);//10,20,30
}
/ / output arr2
System.out.println("==== Elements of arr2 ====");
for(int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);//100,20,30
}
Copy the code