How to copy elements from one array to another in Java

In this tutorial, you will learn how to copy elements from one array to another. There are four ways to do this. Let us look at each method in the next section.

Copying elements from one array to another in Java:

We will see:

  • Copy all the elements
  • Copy all elements except the last one from an array
  • Copy elements of a specific range from an array in Java and store in another array

Using for loop:

Here you need to iterate through each element of the source array using a for loop and copy one element at a time to the destination array.

Java code:

public class copyArray {
  public static void main(String[] args) {
    int[] arr = {5, 4, 3, 2, 1};
    int[] copyArr = new int[arr.length];
    
    //For loop to copy the content of array 'arr' to array 'copyArr'
    for(int i = 0; i<arr.length; i++)
       copyArr[i] = arr[i];
    
    System.out.println("The original array is: ");
    for (int i = 0; i < arr.length; i++)
        System.out.print(arr[i] + " ");
  
    System.out.println("\n\nContents of the copied array is");
    for (int i = 0; i < copyArr.length; i++)
        System.out.print(copyArr[i] + " ");
  }
}

Output:

The original array is: 
5 4 3 2 1

Content of the copied array is:
5 4 3 2 1

Using arraycopy() method:

You can use the System.arraycopy() method, present in java.lang package. The syntax is:

arraycopy(Object sourceArray[], int sourcePos, Object destination[], int destPos, int length)

where,

  • sourceArray is the source array from which elements are copied,
  • sourcePos is the index of the source array from which the copying starts,
  • destination is the destination array,
  • destPos is the index of the destination array from which the elements are placed,
  • length is the length of the elements copied.

Java code:

public class copyArray {
  public static void main(String[] args) {
    int[] arr = {5, 4, 3, 2, 1};
    int[] copyArr = new int[arr.length-1];
      
    System.arraycopy(arr, 0, copyArr, 0, arr.length-1);
    
    System.out.println("The original array is: ");
    for (int i = 0; i < arr.length; i++)
        System.out.print(arr[i] + " ");
  
    System.out.println("\n\nAfter copying, the content of the copied array is:");
    for (int i = 0; i < copyArr.length; i++)
        System.out.print(copyArr[i] + " ");		
  }
}

Output:

The original array is: 
5 4 3 2 1

After copying, the content of the copied array is:
5 4 3 2

In the above example, the source array is arr, and the destination array is copyArr, the source index is 0, the destination index is 0, and the number of elements copied is the length of the source array -1. Therefore, the first 4 elements of the source array get copied into the destination array.

Using copyOfRange() method:

This method is present in the Arrays class. It copies the specified range of the source array into a destination array and returns the array. Syntax:

Object dest[] = Arrays.copyOfRange​(Object source[], int start, int end)

where,

  • source[] is the array from which the elements are copied,
  • dest[] is the array that contains the copied elements,
  • start is the starting index of the range to be copied,
  • end is the final index of the range to be copied, which is exclusive.

Java code:

import java.util.Arrays;

public class copyArr {
  public static void main(String[] args) {
    int[] arr = {5, 4, 3, 2, 1};
    
    int[] copyArr = Arrays.copyOfRange(arr, 1, 4);

    System.out.println("The original array is: ");
    for (int i = 0; i < arr.length; i++)
        System.out.print(arr[i] + " ");
  
    System.out.println("\n\nAfter copying, the content of the copied array is:");
    for (int i = 0; i < copyArr.length; i++)
        System.out.print(copyArr[i] + " ");
    
  }
}

Output:

The original array is: 
5 4 3 2 1

After copying, the content of the copied array is:
4 3 2

In the above program, the starting index of the range is 1, and the ending index is 4. Therefore, the elements within that range, {4, 3, 2}, are copied from the source array into the destination array.

Using copyOf() method:

This method is from the Arrays class and returns an array that contains the copied elements. You can use this method if you want to copy either the first few elements of the array or the complete array. Syntax:

Object dest[] = Arrays.copyOf​(Object source[], int length)

where,

  • source[] is the array from which the elements are copied,
  • dest[] is the array that contains the copied elements,
  • length is the length of the subarray that is to be copied.

Java code:

import java.util.Arrays;

public class copyArr {
  public static void main(String[] args) {
    int[] arr = {5, 4, 3, 2, 1};
    
    int[] copyArr = Arrays.copyOf(arr, 2);

    System.out.println("The original array is: ");
    for (int i = 0; i < arr.length; i++)
        System.out.print(arr[i] + " ");
  
    System.out.println("\n\nAfter copying, the content of the copied array is:");
    for (int i = 0; i < copyArr.length; i++)
        System.out.print(copyArr[i] + " ");
    
  }
}

Output:

The original array is: 
5 4 3 2 1

After copying, the content of the copied array is:
5 4

In the above example, the length of the elements to be copied is given as 2. Therefore, the first two elements from the source array are copied into the destination array.

This is how you copy elements from one array to another. Hope this tutorial helped!

Leave a Reply

Your email address will not be published. Required fields are marked *