dsa

Java Copy Arrays

In this tutorial, you will learn about different ways you can use to copy arrays (both one dimensional and two-dimensional) in Java with the help of examples.

Given an array, we need to copy its elements in a different array.

Copying Arrays Using Assignment Operator

Example:

// A Java program to demonstrate copying by one by one 
// assigning elements of a[] to b[].
public   class Main {
    public static void main(String[] args) {
       
        int [] numbers = {6,7,8,3,4,9};
        int [] positiveNumbers = numbers;    // copying arrays
        // change value of first array
        numbers[0] = -1;
        for (int number: positiveNumbers) {
            System.out.print(number + ", ");
        }
    }
}
   output: 
   -1
   7
   8
   3
   4
   9

In the above example, we have used the assignment operator (=) to copy an array named numbers to another array named positiveNumbers.

2. Using Looping Construct to Copy Arrays

Example:

       
 // A Java program to demonstrate copying by one by one 
// assigning elements of a[] to b[]. 
public class Test 
{ 
    public static void main(String[] args) 
    { 
        int a[] = {1, 7, 3}; 
  
        // Create an array b[] of same size as a[] 
        int b[] = new int[a.length]; 
  
        // Copy elements of a[] to b[] 
        for (int i=0; i<a.length; i++) 
            b[i] = a[i]; 
        /* Change b[] to verify that b[] is different */
        // from a[] 

        b[0]++; 
  
        System.out.println("Contents of a[] "); 
        for (int i=0; i<a.length; i++)
          System.out.print(a[i] + " "); 
  
        System.out.println("\n\nContents of b[] "); 
        for (int i=0; i<b.length; i++) 
            System.out.print(b[i] + " "); 
    } 
} 
Output:

Contents of a[] 
1 7 3 

Contents of b[] 
2 7 3  

In the above example, we have used the for loop to iterate through each element of the source array. In each iteration, we are copying elements from the source array to the destination array.

3. Copying Arrays Using arraycopy() method

The arraycopy() method allows you to copy a specified portion of the source array to the destination array.
For example,

arraycopy(Object src, int srcPos,Object dest, int destPos, int length)

Example:

       
 // A Java program to demonstrate array copy using 
// System.arraycopy() 
public class Test 
{ 
    public static void main(String[] args) 
    { 
        int a[] = {1, 7, 3}; 
  
        // Create an array b[] of same size as a[] 
        int b[] = new int[a.length]; 
  
        // Copy elements of a[] to b[] 
        System.arraycopy(a, 0, b, 0, 3); 
  
        // Change b[] to verify that b[] is different 
        // from a[] 
        b[0]++; 
  
        System.out.println("Contents of a[] "); 
        for (int i=0; i<a.length; i++) 
            System.out.print(a[i] + " "); 
  
        System.out.println("\n\nContents of b[] "); 
        for (int i=0; i<b.length; i++) 
            System.out.print(b[i] + " "); 
    } 
} 
Output:

Contents of a[] 
1 7 3 

Contents of b[] 
2 7 3  

System.arraycopy(n1, 2, n3, 1, 2) - 2 elements of the n1 array starting from index 2 are copied to the index starting from 1 of the n3 array

4. Copying Arrays Using copyOfRange() method

The copyOfRange() method allows you to copy a specified portion of the source array to the destination array.
For example,

int[] destination1 = Arrays.copyOfRange(source, 0, source.length);

Example:

       
 // A Java program to demonstrate array copy using 
// System.copyOfRange()
public class ArraysCopy {
    public static void main(String[] args) {
      
        int[] source = {2, 3, 12, 4, 12, -2};
      
        // copying entire source array to destination
        int[] destination1 = Arrays.copyOfRange(source, 0, source.length);      
        System.out.println("destination1 = " + Arrays.toString(destination1)); 
      
        // copying from index 2 to 5 (5 is not included) 
        int[] destination2 = Arrays.copyOfRange(source, 2, 5); 
        System.out.println("destination2 = " + Arrays.toString(destination2));   
    }
}
Output:

destination1 = [2, 3, 12, 4, 12, -2]
destination2 = [12, 4, 12] 

Here, we can see that we are creating the destination1 array and copying the source array to it at the same time. We are not creating the destination1 array before calling the copyOfRange() method

5. Copying Arrays Using Clone() method

int[] destination2 = destination1.clone();

Example:

       
 // A Java program to demonstrate array copy using clone() 
public class Test 
{ 
    public static void main(String[] args) 
    { 
        int a[] = {1, 8, 3}; 
  
        // Copy elements of a[] to b[] 
        int b[] = a.clone(); 
  
        // Change b[] to verify that b[] is different 
        // from a[] 
        b[0]++; 
  
        System.out.println("Contents of a[] "); 
        for (int i=0; i < a.length; i++) 
            System.out.print(a[i] + " "); 
  
        System.out.println("\n\nContents of b[] "); 
        for (int i=0; i < b.length; i++) 
            System.out.print(b[i] + " "); 
    } 
} 
Output:

Contents of a[] 
1 8 3 

Contents of b[] 
2 8 3  

Here, we can see that we are creating t copying the source array to it at the same time. By using clone() methord.