System.arraycopy – provided since version 1.0. It copies an array from a source array to a destination array. It starts the copy from the source position to the target position with the specified length.
Object.clone() – provided since version 1.0. It creates and returns a copy of the object. If the object is an array, then the array is cloned into a new array with the same content.
Arrays.copyOf – provided since version 1.6. It copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length.
Arrays.copyOfRange – provided since version 1.6. It copies the specified range of the specified array into a new array.
Stream.toArray – provides since version 1.8. It returns an array containing the elements of this stream.
In this example, I will demonstrate how to use these methods to copy an Integer, String, and Object array and check if it is a shallow copy or deep copy when copying an Object array. I will also demonstrate how to use these methods to copy a two-dimensional (2D) Integer array.
In this step, I will create a DemoObject class which has data members, getters, setters, and constructors.
In this step, I will create several Junit test classes to demonstrate array copy for an Integer, String, and Object array. The array copy for other primitive data types: boolean, char, byte, short, long, double, and float are similar to the int type provided in this example.
In this step, I will create a TestBase class to hold the common test data and validation methods.
copyStartAtZeroIndex – set the copy to start at the 0 index of the source array.
sourceStringArray – an array with three String values: "Mary", "Zheng", and "Test". It is used as a source array for the String type.
sourceIntegerArray – an Integer array with five values: 1, 2, 3, 4, 5.
source2DArray – a 2D Integer array.
maryObject – a DemoObject whose name value is "Mary".
zhengObject – a DemoObject whose name value is "Zheng".
sourceObjectArray – an array with maryObject, zhengObject, and null. It is used as a source object array.
validate_IntArray – validate clonedArray has a different object reference from sourceIntegerArray. Validate clonedArray has the same value for each element at the same index. Validate changing either clonedArray or sourceIntegerArray does not affect each other.
validate_StringArray – validate clonedArray has a different object reference from sourceStringArray. Validate clonedArray has the same value for each element at the same index. Validate changing either clonedArray or sourceStringArray does not affect each other.
validate_shallowCopy_ObjectArray – during shallow copy, the copied array's elements hold the same object reference to the source array's elements, so changing the array's elements on either copy or source affects each other.
validate_deepCopy_ObjectArray – during deep copy, the copied array's elements have a different object reference from the source array's elements, so changing the copied element doesn't affect the source's element, and vice versa.
In this step, I will create several test methods to copy an Integer, String, and Object array then validate copied data. It also validates that copying an object array is a shallow copy. I will also use it to copy a 2D int array.
copy_IntArray – creates a copy of an integer array and validates that the copied data is same as the source data.
copy_2d_IntArray – creates a copy of a 2D integer array.
copy_StringArray – creates a copy of a String array and validates that the copied data is same as the source data.
copy_objectArray – creates a copy of an object array and validates that the copied array's element contains the same object reference.
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.
copy_IntArray – it copies an integer array with a specified index position.
copy_2d_IntArray – it creates a copy of a 2D integer array.
copy_StringArray – it copies a String array.
copy_ObjectArray – it copies an object array with a specified index position. It validates that the array copy is a shallow copy.
exception_when_destination_is_null – it throws NullPointerException when the destination is a null object.
exception_when_copy_length_exceed_destination – it throws ArrayIndexOutOfBoundsException when the destination size is smaller than the specified range.
exception_when_copy_length_exceed_source – it throws ArrayIndexOutOfBoundsException when the copy range exceeds the original array index boundary.
copy_IntArray_big – creates a copy of an integer array with a larger size and validates that the copied data is the same as the source data.
copy_IntArray_small – creates a copy of an integer array with a smaller size and validates that the copied data is the same as the source data.
copy_objectArray – creates a copy of an object array and validates that the copied array's elements contain the same object reference.
copy_IntArray – it creates a copy of an integer array and validates that the copied data is the same as the source data.
copy_objectArray – it creates a copy of an object array and validates that the copied array's elements contain the same object reference.
In this step, I will create a test class to copy an array.
copy_IntArray – creates a copy of an integer array and validates that the copied data is the same as the source data.
copy_2d_IntArray_lambda – creates a copy of a 2D integer array with the lambda expression.
copy_2d_IntArray_methodReference – creates a copy of a 2D integer array with the method reference.
copy_StringArray – copies a String array.
copy_objectArray – creates a copy of an object array and validates that the copied array's elements contain the different object reference.
I will execute the Junit tests and capture the results.
In this example, we demonstrated how to copy an array with five methods: System.arraycopy, Object.clone, Arrays.copyOf, Arrays.copyOfRange, and Stream.toArray.
Stream.toArray is a deep copy when copying an object array. The other four methods are a "shallow copy" when copying an object array.
This example consists of a Maven project to copy an array.