1. 概要

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. システムクラス

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.

ターゲット配列にコピーされる要素の数は、指定された長さに等しくなります。 配列のサブシーケンスを別の配列にコピーする簡単な方法を提供します。

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. 配列クラス

Arrays クラスは、配列を別の配列にコピーするための複数のオーバーロードされたメソッドも提供します。 Internally, it uses the same approach provided by the System class that we previously examined. これは主に、 copyOf(…) copyRangeOf(…)の2つのメソッドを提供します。

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. 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.

一方、同じ方法を使用して非プリミティブ型の配列を複製すると、結果は異なります。

囲まれたオブジェクトのクラスがCloneableインターフェイスを実装し、 clone()メソッドをオーバーライドする場合でも、非プリミティブ型の配列要素の浅いコピーを作成します。 Objectクラス。

例を見てみましょう:

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. StreamAPIの使用

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. 外部ライブラリ

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>

テストケースを見てみましょう。

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()));

このクラスでは、各オブジェクトがSerializableインターフェースを実装する必要があります。 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. 結論

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. プリミティブ型の配列を使用している限り、SystemおよびArraysクラスによって提供される任意のメソッドを使用できます。 パフォーマンスに違いはないはずです。

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.