How to copy one array to another in java – Java Program to Copy an Array to Another Array

How to copy one array to another in java: In the previous article, we have seen Java Program to Find all the Leaders in the Array

In this article we are going to see how we can copy one array to another.

Java Program to Copy an Array to Another Array

Java copy array to another: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to copy one array to another.

Method-1: Java Program to Copy an Array to Another Array By Using Variable Assignment Method

Approach:

  • Create an array.
  • Create another array and assign the previous array to it.
  • Display both the arrays.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        //Original array
        int arr[] = {12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68,10,20,30};
        // Copied th original array
        int copyArray[] = arr;
        // Printing both the arrays
        System.out.println("Original Array : "+Arrays.toString(arr));
        System.out.println("Copy Array : "+Arrays.toString(copyArray));
    }
}


Output:

Original Array : [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68, 10, 20, 30]
Copy Array : [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68, 10, 20, 30]

Method-2: Java Program to Copy an Array to Another Array By Copying Elements Individually

Approach:

  • Create two arrays of same size.
  • Use a for loop to copy each element
  • Display both arrays.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        //Original array
        int arr[] = {12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68,10,20,30};
        // Creating the second array of same size
        int copyArray[] = new int[arr.length];

        // Copying each element from the original array
        for(int i=0;i<arr.length;i++)
        {
            copyArray[i] = arr[i];
        }
        // Printing both the arrays
        System.out.println("Original Array : "+Arrays.toString(arr));
        System.out.println("Copy Array : "+Arrays.toString(copyArray));
    }
}
Output:

Original Array : [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68, 10, 20, 30]
Copy Array : [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68, 10, 20, 30]

Method-3: Java Program to Copy an Array to Another Array By Using Clone Method

Approach:

  • Create an array.
  • Create another array and clone the original array to it.
  • Display both the arrays.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        //Original array
        int arr[] = {12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68,10,20,30};
        // Creating the second array by cloning the original array
        int copyArray[] = arr.clone();
        // Printing both the arrays
        System.out.println("Original Array : "+Arrays.toString(arr));
        System.out.println("Copy Array : "+Arrays.toString(copyArray));
    }
}


Output:

Original Array : [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68, 10, 20, 30]
Copy Array : [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68, 10, 20, 30]

Method-4: Java Program to Copy an Array to Another Array By Using Arrays.copy( ) Method

Approach:

  • Create two arrays of same size.
  • Use Arrays.copy( )
  • Display both arrays.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        //Original array
        int arr[] = {12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68,10,20,30};
        // Creating the second array of same size
        int copyArray[] = new int[arr.length];
        // Copying array using  arraycopy()
        System.arraycopy(arr, 0, copyArray, 0, arr.length);
        // Printing both the arrays
        System.out.println("Original Array : "+Arrays.toString(arr));
        System.out.println("Copy Array : "+Arrays.toString(copyArray));
    }
}

Output:

Original Array : [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68, 10, 20, 30]
Copy Array : [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68, 10, 20, 30]

Method-5: Java Program to Copy an Array to Another Array By Using Arrays.copyOf( ) Method

Approach:

  • Create an array.
  • Create another array and use Arrays.copyOf( ) to copy all elements.
  • Display both the arrays.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        //Original array
        int arr[] = {12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68,10,20,30};
        // Creating the second array and copying elements
        int copyArray[] = Arrays.copyOf(arr,arr.length);

        // Printing both the arrays
        System.out.println("Original Array : "+Arrays.toString(arr));
        System.out.println("Copy Array : "+Arrays.toString(copyArray));
    }
}

Output:

Original Array : [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68, 10, 20, 30]
Copy Array : [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68, 10, 20, 30]

Method-6: Java Program to Copy an Array to Another Array By Using Arrays.copyOfRange( ) Method

Approach:

  • Create an array.
  • Create another array and use Arrays.copyOfRange( ) to copy all elements.
  • Display both the arrays.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        //Original array
        int arr[] = {12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68,10,20,30};
        // Creating the second array and copying elements
        int copyArray[] = Arrays.copyOfRange(arr,0,arr.length);

        // Printing both the arrays
        System.out.println("Original Array : "+Arrays.toString(arr));
        System.out.println("Copy Array : "+Arrays.toString(copyArray));
    }
}

Output:

Original Array : [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68, 10, 20, 30]
Copy Array : [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68, 10, 20, 30]

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Related Java Programs: