Java-Programming-Docs Help

Copying and Cloning Arrays

You can do many things when working with arrays. If you've outgrown the initial size of the array, you need to create a new larger one and copy the original elements into the same location of the larger array. If, however, you don't need to make the array larger, but instead you want to modify the array's elements while keeping the original array intact, you must create a copy or clone of the array.

The arraycopy() method of the System class allows you to copy elements from one array to another. When making this copy, the destination array can be larger; but if the destunation is smaller, an ArrayIndexOutOfBoundException will be thrown at runtime. The arraycopy method takes five arguments ( two for each array and starting position, and one for the number of elements to copy ):

public static void arraycopy(Object sourceArray, int sourceOffset, Object destinationOffset, int numberOfElementsToCopy);

Besides type compatibility, the only requirement here is that the destination array's memory is already allocated

To demonstrate, take an integer array and creates a new array that is twice as large. The doubleArray() method in the following example does this for us:

Doubling the size of an array

package cloud.yebei.java.collections.arrays; public class DoubleArray { public static void main(String[] args) { int[] array1 = {1, 2, 3, 4, 5}; int[] array2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; System.out.println("Original array 1 size: " + array1.length); System.out.println("New array 1 size: " + doubleArray(array1).length); System.out.println("Original array 2 size: " + array2.length); System.out.println("New array 2 size: " + doubleArray(array2).length); } static int[] doubleArray(int[] originalArray) { int length = originalArray.length; int[] newArray = new int[length * 2]; System.arraycopy(originalArray, 0, newArray, 0, length); return (newArray); } }

After getting the length of the original array, a new array of the right size is created before the old elements are copied into their original positions in the new array. After you learn about array reflection, you can generalize the method to double the size of an array of any type.

When executed, the program generates the following output:

Original array 1 size: 5 New array 1 size: 10 Original array 2 size: 10 New array 2 size: 20

This works even if there is some overlap. Since arrays implement the Cloneable interface, besides copying regions of arrays, you can also clone them. Cloning involves creating a new array of the same size and type and copying all the old elements into the new array. This is unlike copying, which requires you to create and size the destination array yourself. In the case of primitive elements, the new array has copies of the old elements, so changes to the elements of one are not reflected in the copy. However, in the case of object references, only the reference is copied. Thus, both copies of the array would point to the same object. Changes to that object would be reflected in both arrays. This is called a shallow copy or shallow clone

To demonstrate, the following method takes one integer array and returns a copy of said array

static int[] cloneArray(int[] original) { return (int[] original.clone()); }
    Last modified: 16 July 2024