Opening Hours :7AM to 9PM
public class ArrayCopyExample { public static void main(String[] args) { int[] array1 = {1, 2, 3, 4, 5}; int[] array2 = new int[array1.length]; // copy all elements of array1 to array2 for (int i = 0; i < array1.length; i++) { array2[i] = array1[i]; } // print out the contents of array2 for (int i : array2) { System.out.print(i + " "); } } }In this example, array1 is an integer array containing the values {1, 2, 3, 4, 5}. array2 is a new integer array with the same length as array1. The first loop copies all elements of array1 to array2, and the second loop prints out the contents of array2.
public class ArrayCopyExample { public static void main(String[] args) { int[] array1 = {1, 2, 3, 4, 5}; int[] array2 = new int[array1.length]; // copy all elements of array1 to array2 System.arraycopy(array1, 0, array2, 0, array1.length); // print out the contents of array2 for (int i : array2) { System.out.print(i + " "); } } }It will also copy the same content of array1 to array2. This function is more efficient than using a loop as it is optimized in C, so it runs faster and is more memory efficient.