If you’re working with Spring, check out "REST With Spring":

>> CHECK OUT THE COURSE

1. Overview

In this quick article, we’ll discuss different array copying methods in Java. Array copy may seem like a trivial task, but it may cause unexpected results and program behaviors if not done carefully.

2. The System Class

Let’s start with the core Java library – System.arrayCopy(); this copies an array from a source array to a destination array, starting the copy action from the source position to the target position till the specified length.

The number of elements copied to the target array equals the specified length. It provides an easy way to copy a sub-sequence of an array to another.

If any of the array arguments is null, it throws a NullPointerException and if any of the integer arguments is negative or out of range, it throws an IndexOutOfBoundException.

Let’s have a look at an example to copy a full array to another using the java.util.System class:

int[] array = {23, 43, 55};
int[] copiedArray = new int[3];

System.arraycopy(array, 0, copiedArray, 0, 3);

Arguments this method take are; a source array, the starting position to copy from source array, a destination array, the starting position in the destination array, and the number of elements to be copied.

Let’s have a look at another example that shows copying a sub-sequence from a source array to a destination:

int[] array = {23, 43, 55, 12, 65, 88, 92};
int[] copiedArray = new int[3];

System.arraycopy(array, 2, copiedArray, 0, 3);
assertTrue(3 == copiedArray.length);
assertTrue(copiedArray[0] == array[2]);
assertTrue(copiedArray[1] == array[3]);
assertTrue(copiedArray[2] == array[4]);

3. The Arrays Class

The Arrays class also offers multiple overloaded methods to copy an array to another. Internally, it uses the same approach provided by System class that we have seen earlier. It mainly provides two methods, copyOf(…) and copyRangeOf(…).

Let’s have a look at copyOf first:

int[] array = {23, 43, 55, 12};
int newLength = array.length;

int[] copiedArray = Arrays.copyOf(array, newLength);

It’s important to note that Arrays class uses Math.min(…) for selecting the minimum of the source array length and the value of the new length parameter to determine the size of the resulting array.

Arrays.copyOfRange() takes 2 parameters, ‘from’ and ‘to’ in addition to the source array parameter. The resulting array includes the ‘from’ index but the ‘to’ index is excluded. Let’s see an example:

int[] array = {23, 43, 55, 12, 65, 88, 92};

int[] copiedArray = Arrays.copyOfRange(array, 1, 4);
assertTrue(3 == copiedArray.length);
assertTrue(copiedArray[0] == array[1]);
assertTrue(copiedArray[1] == array[2]);
assertTrue(copiedArray[2] == array[3]);

Both of these methods do a shallow copy of objects if applied on an array of non-primitive object types. Let’s see an example test case:

Employee[] copiedArray = Arrays.copyOf(employees, employees.length);

employees[0].setName(employees[0].getName() + "_Changed");
 
assertArrayEquals(copiedArray, array);

Because the result is a shallow copy – a change in the employee name of an element of the original array caused the change in the copy array.

And so – if we want to do a deep copy of non-primitive types – we can go for the other options described in the upcoming sections.

4. Array Copy with Object.clone()

Object.clone() is inherited from Object class in an array.

Let’s first copy an array of primitive types using clone method:

int[] array = {23, 43, 55, 12};
 
int[] copiedArray = array.clone();

And a proof that it works:

assertArrayEquals(copiedArray, array);
array[0] = 9;

assertTrue(copiedArray[0] != array[0]);

The above example shows that have the same content after cloning but they hold different references, so any change in any of them won’t affect the other one.

On the other hand, if we clone an array of non-primitive types using the same method, then the results will be different.

It creates a shallow copy of the non-primitive type array elements, even if the enclosed object’s class implements the Cloneable interface and overrides the clone() method from the Object class.

Let’s have a look at an example:

public class Address implements Cloneable {
    // ...

    @Override
    protected Object clone() throws CloneNotSupportedException {
         super.clone();
         Address address = new Address();
         address.setCity(this.city);
        
         return address;
    }
}

We can test our implementation by creating a new array of addresses and invoking our clone() method:

Address[] addresses = createAddressArray();
Address[] copiedArray = addresses.clone();
addresses[0].setCity(addresses[0].getCity() + "_Changed");
assertArrayEquals(copiedArray, addresses);

This example shows that any change in the original or copied array would cause the change in the other one even when the enclosed objects are Cloneable.

5. Using the Stream API

It turns out, we can use the Stream API for copying arrays too. Let’s have a look at an example:

String[] strArray = {"orange", "red", "green'"};
String[] copiedArray = Arrays.stream(strArray).toArray(String[]::new);

For the non-primitive types, it will also do a shallow copy of objects. To learn more about Java 8 Streams, you can start here.

6. External Libraries

Apache Commons 3 offers a utility class called SerializationUtils that provides a clone(…) method. It is very useful if we need to do a deep copy of an array of non-primitive types. It can be downloaded from here and its Maven dependency is:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.5</version>
</dependency>

Let’s have a look at a test case:

public class Employee implements Serializable {
    // fields
    // standard getters and setters
}

Employee[] employees = createEmployeesArray();
Employee[] copiedArray = SerializationUtils.clone(employees);
employees[0].setName(employees[0].getName() + "_Changed");
assertFalse(
  copiedArray[0].getName().equals(employees[0].getName()));

This class requires that each object should implement the Serializable interface. In terms of performance, it is slower than the clone methods written manually for each of the objects in our object graph to copy.

7. Conclusion

In this tutorial, we had a look at the various options to copy an array in Java.

The method to use is mainly dependent upon the exact scenario. As long as we’re using a primitive type array, we can use any of the methods offered by the System and Arrays classes. There shouldn’t be any difference in performance.

For non-primitive types, if we need to do a deep copy of an array we can either use the SerializationUtils or add clone methods to our classes explicitly.

And as always, the examples shown in this article are available on over on GitHub.

The new Certification Class of "REST With Spring" is finally out:

>> CHECK OUT THE COURSE

  • Viktor Schmidt (viktorianer)

    I don’t think that using external libraries is the only one solution for deep copy – because Cloneable does not do what you expect from it.

    “For non-primitive types, if we need to do a deep copy of an array we can … add clone methods to our classes explicitly.”

    As it is written in paragraph above this is not the best solution.

    You can write instead a copy constructor or provide deep copy factory (see Effective Java, Override clone judiciously, Joshua Bloch).

    • Grzegorz Piwowarek

      Of course it can be done. The problem is that in practical contexts it’s a bit cumbersome to spend that much time on thinking about proper cloning. This is why people prefer to reach for an external library in such case