Copying an Array in Java

Copy Array in Java

Different Ways to Copy an Array

An array is a collection of data elements with similar data type stored at contiguous memory locations. We can copy an array by using various operations on them. There are two different types of array 1D and 2D array.

Here, in the page we will discuss more about the copying of an Array that are used in java.

Copy Array in Java:

In Java, We can copy an array to another by different ways and inbuilt methods which are already defined in the collection framework of java.

array copy

We can copy 1-D array by using:

  • Assignment Operator (=)
  • Looping Construct to Copy Arrays
  • arraycopy() method
  • copyOfRange() method
  • clone() method

We can copy 2-D array by:

  • 2d Arrays Using Loop
  • using arraycopy()

Assignment Operator:

we can copy the entire data of an array with the help of assignment operator(=). This is the easiest way to copy the whole array

Example:
Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        // Initializing an array
        int[] arr = {1,2,3,4,5,6};

        // Copy array using assignment operator
        int[] copy = arr;
        System.out.print("Copied Array is : ");

        // Printing the arrays using loop
        for(int i = 0;i<copy.length;i++){
            System.out.print(copy[i] + " ");
        }
    }
}
Output:
Copied Array is : 1 2 3 4 5 6

Using Looping:

we can also copy an array by iterating over the elements of the array and copy every element to the different array.

Example:
Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        // Initializing yhe arrays
        int[] arr = {1,2,3,4,5,6};
        int[] copy = new int[arr.length];
        System.out.print("Copied Array : "); 

        // Copying the array using loops
        for(int i = 0;i<copy.length;i++){
            copy[i] = arr[i]; 
        }

        // Printing the array
        for(int i = 0;i<copy.length;i++){
            System.out.print(copy[i] + " "); 
        }
    }
}
Output:
Copied Array : 1 2 3 4 5 6

Using arraycopy() Method:

In Java, the System class contains arraycopy() method to copy arrays. This method is much better than the other approaches to copy arrays than the others

Syntax:
System.arraycopy(source_array,Source_Position,Dest_Array,Dest_Position,Length)
Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        // Initializing the array
        int[] arr = {1,2,3,4,5,6};
        int[] copy = new int[arr.length];
        System.out.print("Copies Array : ");

        // Copy the array using arraycopy 
        System.arraycopy(arr,0,copy,0,arr.length);

        // Printing the array usinng loops
        for(int i = 0;i<copy.length;i++){
            System.out.print(copy[i] + " "); 
        }
    }
}
Output:
Copied Array : 1 2 3 4 5 6

Using copyofRange() Method:

We can also use the copyOfRange() method defined in Java Arrays class to copy arrays.

Syntax:
System.Arrays.copyOfRange(source_array,0,Source.Length)
Run
import java.util.Arrays;

public class Main {
    public static void main(String[] args){
        // Initializing an array
        int[] array = {1, 2, 3, 4, 5};
        
        // copy array using copyofRange for a given range
        int[] newArray = Arrays.copyOfRange(array, 1, 4);

        // Printing the copied array
        System.out.print("Copied Array: ");
        for(int i = 0;i < newArray.length; i++){
            System.out.print(newArray[i] + " ");
        }
    }
}
Output:
Copied Array : 2 3 4

Using clone() Method:

We can also use the clone() method defined in Java object class to copy arrays.

Syntax:
Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        // Initializing the array
        int[] arr = {1,2,3,4,5,6};

        // Copy the whole array using clone function
        int[] copy = arr.clone();
        System.out.print("Copies Array : ");

        // Printing the copies array 
        for(int i = 0;i < copy.length; i++){
            System.out.print(copy[i] + " "); 
        }
    }
}
Output:

Copied Array : 1 2 3 4 5 6

Copy a 2-D Arrays using Loops:

Just like the 1-D array, we can also copy the 2-D array by using loops.

Example:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        int[][] arr = {{1,2,3},{4,5,6}};
        int[][] copy = new int[2][3]; 
        System.out.println("Copies Array : "); 
        for(int i = 0;i<copy.length;i++){
            for(int j = 0;j<copy[i].length;j++){
                copy[i][j] = arr[i][j];
            }
        }
        for(int i = 0;i<copy.length;i++){
            for(int j = 0;j<copy[i].length;j++){
                System.out.print(copy[i][j] + " "); 
            }
            System.out.println();
        }
    }
}
Output:
Copies Array : 
1 2 3 
4 5 6 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription