Copying Arrays in Java

In this article, we will learn different ways to copy an array in Java. Java provides inbuilt methods to copy the array. Whether you want a full copy or partial copy of the array, you can do it easily using java inbuilt classes.
In this article, we will see four different ways to copy an array in Java.
  1. Copying Arrays using Built-in System.arraycopy() Method
  2. Copying Arrays Using Arrays.copyOf() Method
  3. Copying Arrays using Object.clone() method
  4. Copying Arrays using Arrays.copyOfRange()

1. Copying Arrays using Built-in System.arraycopy() Method

The following program, ArrayCopyDemo, declares an array of char elements, spelling the word "decaffeinated." It uses the System.arraycopy method to copy a subsequence of array components into a second array:

arraycopy() method

public static void arraycopy(Object src,
                             int srcPos,
                             Object dest,
                             int destPos,
                             int length)
Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components is copied from the source array referenced by src to the destination array referenced by dest.
Parameters:
  • src - the source array.
  • srcPos - starting position in the source array.
  • dest - the destination array.
  • destPos - starting position in the destination data.
  • length - the number of array elements to be copied.
Throws:
  • IndexOutOfBoundsException - if copying would cause access of data outside array bounds.
  • ArrayStoreException - if an element in the src array could not be stored into the dest array because of a type mismatch.
  • NullPointerException - if either src or dest is null.

/**
 * This class shows different methods for copy array in java
 * @author javaguides.net
 *
 */

class ArrayCopyDemo {
    public static void main(String[] args) {
        char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
                            'i', 'n', 'a', 't', 'e', 'd' };
        char[] copyTo = new char[7];

        System.arraycopy(copyFrom, 2, copyTo, 0, 7);
        System.out.println(new String(copyTo));
    }
}
The output from this program is:
caffein
Let's see one more example using integer array.
/**
 * This class shows different methods for copy array in java
 * @author javaguides.net
 *
 */
public class JavaArrayCopyExample {

        public static void main(String[] args) {
                int[] source = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                
                System.out.println("Source array = " + Arrays.toString(source));

                int[] temp = new int[5];
                System.arraycopy(source, 0, temp, 0, 5);
                
                System.out.println(
                                "Copy First five elements of array. Result array = " + Arrays.toString(temp));
      }
}
Output:
Source array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Copy First five elements of array. Result array = [1, 2, 3, 4, 5]

2. Copying Arrays Using Arrays.copyOf() Method

If you want to copy first few elements of an array or full copy of array, you can use this method. Obviously it’s not versatile like System.arraycopy() but it’s also not confusing and easy to use. This method internally use System arraycopy() method.
import java.util.Arrays;

/**
 * This class shows different methods for copy array in java
 * @author javaguides.net
 *
 */
public class JavaArrayCopyExample {

        public static void main(String[] args) {
                int[] source = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                
                System.out.println("Source array = " + Arrays.toString(source));

                int[] dest = Arrays.copyOf(source, source.length);
                
                System.out.println(
                                "Copy First five elements of array. Result array = " + Arrays.toString(dest));
       }
}
Output:
Source array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Copy First five elements of array. Result array = [1, 2, 3, 4, 5, 6, 7, 8, 9]

3. Copying Arrays using Object.clone() method

Object class provides clone() method and since an array in java is also an Object, you can use this method to achieve full array copy. This method will not suit you if you want a partial copy of the array.
/**
 * This class shows different methods for copy array in java
 * @author javaguides.net
 *
 */
public class JavaArrayCopyExample {

        public static void main(String[] args) {
                int[] source = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                
                System.out.println("Source array = " + Arrays.toString(source));

                int[] dest = source.clone();
                
                System.out.println(
                                "Copy First five elements of array. Result array = " + Arrays.toString(dest));
       }
}
Output:
Source array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Copy First five elements of array. Result array = [1, 2, 3, 4, 5, 6, 7, 8, 9]

4. Copying Arrays using Arrays.copyOfRange()

If you want a few elements of an array to be copied, where starting index is not 0, you can use this method to copy the partial array. Again this method is also using System arraycopy method itself.
/**
 * This class shows different methods for copy array in java
 * @author javaguides.net
 *
 */
public class JavaArrayCopyExample {

        public static void main(String[] args) {
                int[] source = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                
                System.out.println("Source array = " + Arrays.toString(source));

                int[] dest = Arrays.copyOfRange(source, source.length - 3, source.length);
                
                System.out.println(
                                "Copy First five elements of array. Result array = " + Arrays.toString(dest));
       }
}
Output:
Source array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Copy First five elements of array. Result array = [7, 8, 9]

Related Array Posts

Comments