Java Program to copy all elements of one array into another array

Check if two arrays contain the same elements?

Example:

Input: arr1 = { 4, 6, 1, 8, 1 }
Output: arr2 = { 4, 6, 1, 8, 1 }

Approach: int array 

Java


import java.util.Arrays;

public class CopyArrayTo {
    public static void main(String[] args) {
        // original array
        int[] arr1 = new int[] { 46181 };
        // new array of original array size
        int arr2[] = new int[arr1.length];

        // copy using loop
        for (int i = 0; i < arr1.length; i++) {
            arr2[i] = arr1[i];
        }
        System.out.println("Orginal Array " + Arrays.toString(arr1));
        System.out.println("Copy Array " + Arrays.toString(arr2));
    }
}


No comments:

Post a Comment