1. Overview

In this quick tutorial, we’ll discuss the different array copying methods in Java. Array copying may seem like a trivial task, but it can 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 until 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. If any of the integer arguments is negative or out of range, it throws an IndexOutOfBoundException.

Let’s look at an example of copying 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);

This method takes the following arguments: a source array, the starting position to copy from the source array, a destination array, the starting position in the destination array, and the number of elements to be copied.

Let’s look at another example of 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 the System class that we previously examined. It mainly provides two methods, copyOf(…) and copyRangeOf(…).

Let’s 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 the 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:

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:

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

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

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

If we want to do a deep copy of non-primitive types, we can opt for one of the other options described in the upcoming sections.

4. Array Copy With Object.clone()

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

First, we’ll copy an array of primitive types using the clone method:

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

Here’s proof that it works:

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

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

The above example shows they have the same content after cloning, but they hold different references, so any change in one 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 will cause a change in the other one, even when the enclosed objects are Cloneable.

5. Using the Stream API

As it turns out, we can use the Stream API for copying arrays too:

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

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

6. External Libraries

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

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</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’s slower than the clone methods written manually for each of the objects in our object graph to copy.

7. Conclusion

In this article, we discussed the various options to copy an array in Java.

The method we choose 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.

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


« 上一篇: Java 动态代理用法