Java Program to copy one array to another array

Java Program to copy one array to another array

In this tutorial we are going to learn writing java program to copy one array to another array. This program will be going to very simple.

Here using the for loop by iterating the array elements, we can easily store each element into the second array.

For example:

Suppose we have an array arr1= [1, 2, 3, 4, 5, 6]

And we have a second array arr2 with some size. Now to after copy of array arr1 to arr2,

arr2 will become [1, 2, 3, 4, 5, 6].

This is how array copy works.

Program 1: Java Program to copy array from one to another array

  • In the below program we have built own logic to copy one array to another array.
  • Here we are iterating the input array one by one, and each time assigning the value to other array corresponding to the index.
  • Here we have fixed array on which we are performing the copy.
public class Main {    
    public static void main(String[] args) {        
        int[] arr1 = new int [] {1, 2, 3, 4, 5, 6};     
        int arr2[] = new int[arr1.length];    
        for (int i = 0; i < arr1.length; i++) {     
            arr2[i] = arr1[i];     
        }      
        System.out.println("Elements in arr1 ");    
        for (int i = 0; i < arr1.length; i++) {     
           System.out.print(arr1[i] + " ");    
        }     
        System.out.println("\nElements in arr2 after copy from arr1 ");    
        for (int i = 0; i < arr2.length; i++) {     
           System.out.print(arr2[i] + " ");    
        }     
    }    
}   

Output

copy one array to another array java

Program 2: Java Program to copy array from one to another array (User Input)

  • This is the manual approach of copying one array to another array.
  • Here we are iterating the input array one by one, and each time assigning the value to other array corresponding to the index.
  • The only difference in the below program from the above is, here we are taking array size and value at run time from the user.
import java.util.*;
public class Main {    
    public static void main(String[] args) {        
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the size of array: ");
        int size = sc.nextInt();
        int arr1[] = new int[size];
        int arr2[] = new int[size];
        for(int i=0; i<size; i++) {
            System.out.print("Please give value for index "+ i +" : ");
            arr1[i] = sc.nextInt();
        }
        for (int i = 0; i < arr1.length; i++) {     
            arr2[i] = arr1[i];     
        } 
        System.out.println("Elements in arr1 ");    
        for (int i = 0; i < size; i++) {     
           System.out.print(arr1[i] + " ");    
        }     
        System.out.println("\nElements in arr2 after copy from arr1 ");    
        for (int i = 0; i < size; i++) {     
           System.out.print(arr2[i] + " ");    
        }     
    }    
}    

Output

java program to copy one array to another

Program 3: Using clone() method to copy array in java

  • This is one of the easiest method to clone the array in java.
  • Using clone() method, without writing any logic we can copy one array to another array.
  • Suppose we want to copy arr1 to arr2 ,syntax will be arr2 = arr1.clone().
import java.util.*;
public class Main {    
    public static void main(String[] args) {        
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the size of array: ");
        int size = sc.nextInt();
        int arr1[] = new int[size];
        int arr2[] = new int[size];
        for(int i=0; i<size; i++) {
            System.out.print("Please give value for index "+ i +" : ");
            arr1[i] = sc.nextInt();
        }
         
        arr2 = arr1.clone();
        System.out.println("Elements in arr1 ");    
        for (int i = 0; i < size; i++) {     
           System.out.print(arr1[i] + " ");    
        }     
        System.out.println("\nElements in arr2 after copy from arr1 ");    
        for (int i = 0; i < size; i++) {     
           System.out.print(arr2[i] + " ");    
        }     
    }    
}    

Output

java program to copy one array to another using clone() method

Program 4: Copy array in java using copyOf() method of Arrays class

  • Using copyof() method we can perform array elements copy in java.
  • With the help of copyof() method, we can copy partial array of full array by passing the size in parameter.
  • copyof method looks like copyOf​(int[] original, int newLength)
import java.util.*;
public class Main {    
    public static void main(String[] args) {        
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the size of array: ");
        int size = sc.nextInt();
        int arr1[] = new int[size];
        for(int i=0; i<size; i++) {
            System.out.print("Please give value for index "+ i +" : ");
            arr1[i] = sc.nextInt();
        }
        int arr2[] = Arrays.copyOf(arr1, size);
        System.out.println("Elements in arr1 ");    
        for (int i = 0; i < size; i++) {     
           System.out.print(arr1[i] + " ");    
        }     
        System.out.println("\nElements in arr2 after copy from arr1 ");    
        for (int i = 0; i < size; i++) {     
           System.out.print(arr2[i] + " ");    
        }     
    }    
}  

Output

copy one array to another array in java