How to copy one dimensional array in Java

Copying an array in Java is to copy the elements of one array to another array. The index of the array elements in the destination array is the same as the source array.

Using assignment operator

We can copy an element of one array to another array using an assignment operator.

Let us consider an array a[ ] = {4, 9, 16, 25}. The program below illustrates copying elements of one array to another array.

public static void main(String[] args) {
    int[] a = {4, 9, 16, 25};
    int[] b = a;   // contents of array a is copied to array b
    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] + " ");
  }

The output is

Contents of a[] 
4 9 16 25 

Contents of b[] 
4 9 16 25 

This is the easiest way to copy an array. But there is one problem with this method, if we change the contents of array a[], it will be reflected in array b[] also. This way of copying is also called shallow copying. This method is not a good practice for copying arrays.

Copying array elements using for loop

An array of elements can be copied using for loop. Using for loop, we will iterate through the array and copy one element at a time to the destination array.

If an array is copied using this method and if any array element is modified in the source array, then it will not reflect in the destination array.

Let us take an array source [] = { 15, 30, 45, 60, 75}. The example below illustrates how to copy an array element one by one from source to destination.

public static void main(String[] args) {
    int source[] = {15, 30, 45, 60};
    // Create an array dest[] of same size as source[]
    int dest[] = new int[source.length];
    // Copy elements from source[] to dest[]
    for (int i = 0; i < source.length; i++)
      dest[i] = source[i];
  }

Using System.arraycopy() method

This method is a built-in method of java.lang package. Using this method a source array can be copied to the destination array.

The syntax for this method is

System.arraycopy(Object source, int srcStart, Object dest, int destStart, int length)
  • source - source array
  • srcStart - starting index of the source array
  • dest - destination array
  • destStart - starting index of the destination array
  • length - number of elements to be copied

Let us consider an array a[] = {25, 50, 75, 100, 125}. Example below illustrates how to copy the source array to destination array using System.arraycopy() method.

import java.lang.*;
public class arraycopy {
public static void main(String args[]) {
     int a[] = {25, 50, 75, 100, 125};
     // Creating an array b[] same size as a[]
     int b[] = new int[a.length];
     // Copying elements of a[] to b[]
     System.arraycopy(a, 0, b, 0, 5);
   }
}

Using Object.clone() method

The Object.clone() method is a built-in method of java.lang package. This method creates and returns a copy of the Object passed in the method.

The syntax is

a.clone()

Let us consider an array original[] = {100, 200, 300, 400}. Example below illustrates how to copy array original using Object.clone() method.

import java.util.*;
  class ArrayCopy {
    public static void main(String args[]) {
      int[] original = {100, 200, 300, 400};
      int[] copy = original.clone();
      System.out.println("Original: " + Arrays.toString(original));
      System.out.println("Copy: " + Arrays.toString(copy));
    }
  }

The output is

Original: [100, 200, 300, 400]
Copy: [100, 200, 300, 400]

Using Arrays.copyOf() method

Arrays.copyOf() method is an inbuilt method of the java.util.Arrays Class. It copies the specified array in the method padding with zeroes if necessary to make sure that the copied array is of a specified length.

The syntax is

Arrays.copyOf(int[] original_array, int length)
  • original_array - It is the array to be copied
  • length - It is the length of the new copying array

Let us take a array source [] = { 50, 100, 150, 200, 250}. The program below illustrates how to copy an array using Arrays.copyOf() method.

 import java.util.*;
  public class CopyOf {
    public static void main(String args[]) {
      int[] source = {50, 100, 150, 200, 250};
      int[] dest = Arrays.copyOf(source, source.length);
      System.out.println("Original: " + Arrays.toString(source));
      System.out.println("Copy: " + Arrays.toString(dest));
    }
  }

The output is

Original: [50, 100, 150, 200, 250]
Copy: [50, 100, 150, 200, 250]

Using Arrays.copyOfRange() method

The Arrays.copyOfRange() method is a built-in method of the java.util.Arrays Class. This method copies the specified range of the specified array into the destination array.

The syntax is

Arrays.copyOfRange​(int[] original, int start, int end)
  • original - It is the source array
  • start - It is the starting index of the range to copy
  • end - It is the final index of the range to copy

Let us consider a array a[] = {20, 40, 60, 80}. Example below illustrates the use of Arrays.copyOfRange() method.

public static void main(String args[]) {
  int a[] = {20, 40, 60, 80};
  // Copy elements of a[] to b[]
  int b[] = Arrays.copyOfRange(a, 2, 4);
  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] + " ");
}

The output is

Contents of a[] 
20 40 60 80 

Contents of b[] 
60 80 

To copy a two-dimensional array

Just like a one-dimensional array, we can copy a two-dimensional array also.

Copying 2D array using for loop

Using two for loops both the index of the two-dimensional array is iterated and the elements of the source array are copied to the destination array.

Let us consider an array source[4][3] = {{2, 4, 6}, {8, 10, 12}, {14, 16, 18}, {20, 22, 24}}. The program illustrates how to copy a two-dimensional array.

import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    int[][] source = {{2, 4, 6}, {8, 10, 12}, {14, 16, 18}, {20, 22, 24}};
    System.out.println("Source array:" + Arrays.deepToString(source));
    int[][] destination = new int[source.length][];
    for (int i = 0; i < destination.length; ++i) {
      // allocating space for each row of destination array
      destination[i] = new int[source[i].length];
      for (int j = 0; j < destination[i].length; ++j) {
        destination[i][j] = source[i][j];
      }
    }
    // displaying destination array
    System.out.println("Destination array:" + Arrays.deepToString(destination));
  }
}

The output is

Source array:[[2, 4, 6], [8, 10, 12], [14, 16, 18], [20, 22, 24]]
Destination array:[[2, 4, 6], [8, 10, 12], [14, 16, 18], [20, 22, 24]]

Copying 2D array using System.arraycopy() method

The System.arraycopy() is a built-in method of java.lang package. Using this method source array can be copied to the destination array using System.arraycopy() method.

The syntax is

System.arraycopy(Object source, int srcPos, Object dest, int destPos, int length)
  • source - It is the source array
  • srcPos - starting index of the source array
  • dest - It is the destination array
  • destPos - starting index of the destination array
  • length - number of elements to be copied

Let us consider an array a[3][3] = {{5, 6, 7}, {15, 16, 17}, {25, 26, 27}}. The example program below illustrates how to copy array a[][] using System.arraycopy() method.

import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    int[][] a = {{5, 6, 7}, {15, 16, 17}, {25, 26, 27}};
    System.out.println("Source array:" + Arrays.deepToString(a));
    int[][] b = new int[a.length][];
    for (int i = 0; i < a.length; ++i) {
      // allocating space for each row of destination array
      b[i] = new int[a[i].length];
      System.arraycopy(a[i], 0, b[i], 0, b[i].length);
    }
    // displaying target array
    System.out.println("Destination array:" + Arrays.deepToString(b));
  }
}

The output is

Source array:[[5, 6, 7], [15, 16, 17], [25, 26, 27]]
Destination array:[[5, 6, 7], [15, 16, 17], [25, 26, 27]]

Using Arrays.copyOf() method

The Arrays.copyOf() method is a built-in method of the java.util.Arrays Class. It copies the specified array in the method to the specified destination array according to the length specified.

The syntax is

Arrays.copyOf(int[] original, int length)
  • original - It is the array to be copied
  • length - It is the length of the new copying array

Let us take an array source[3][2] = {{11, 22}, {33, 44}, {55, 66}}. The example below illustrates the copying of array source to array destination.

import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    int[][] source = {{11, 22}, {33, 44}, {55, 66}};
    System.out.println("Source array: " + Arrays.deepToString(source));
    int[][] dest = new int[source.length][];
    for (int i = 0; i < source.length; i++) {
      dest[i] = Arrays.copyOf(source[i], source[i].length);
    }
    System.out.println("Destination array: " + Arrays.deepToString(dest));
  }
}

The output is

Source array: [[11, 22], [33, 44], [55, 66]]
Destination array: [[11, 22], [33, 44], [55, 66]]
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions