Sub topic 8
Copying Arrays and Understanding Shallow Copies
An array is a fixed-size sequence of values. Each value is called an element. Copying an array gives you a new array, so you can change its elements without changing the original array’s elements.
But what an element holds matters. Some elements hold simple values, such as integers. Others hold references. A reference is a value that points to an object, which is data stored in memory. A copy of a reference still points to the same object.
Assignment is not a copy
When you assign one array variable to another, both variables refer to the same array. Changing an element through either variable changes that one array.
To make a separate array, you can use Arrays.copyOf. Its second argument is the new array’s length:
- If the new length is shorter, the extra elements are left out.
- If it is longer, the extra elements get default values. For example, extra
intelements are0, extrabooleanelements arefalse, and extra object references arenull.nullmeans the reference points to no object.
You can also call clone() on an array. This creates a new array of the same length. Both copyOf and clone() make shallow copies, as the next example shows.
Try copying different arrays
A class is a blueprint for objects. The Box class below creates objects that hold an integer. A method is an operation you can call; main is where this program starts.
Save this code as ArrayCopyDemo.java:
import java.util.Arrays;
public class ArrayCopyDemo {
static class Box {
int value;
Box(int value) {
this.value = value;
}
}
public static void main(String[] args) {
int[] scores = {2, 4};
int[] same = scores;
same[0] = 7;
int[] longer = Arrays.copyOf(scores, 3);
longer[0] = 9;
System.out.println("Original score: " + scores[0]);
System.out.println("Copied score: " + longer[0]);
System.out.println("Extra element: " + longer[2]);
Box[] boxes = {new Box(5)};
Box[] boxCopy = boxes.clone();
boxCopy[0].value = 6;
System.out.println("Original box value: " + boxes[0].value);
int[][] rows = {{1, 2}, {3}};
int[][] rowCopy = rows.clone();
rowCopy[0][0] = 8;
System.out.println("Original row value: " + rows[0][0]);
int[][] separateRows = new int[rows.length][];
for (int i = 0; i < rows.length; i++) {
separateRows[i] = rows[i] == null ? null : rows[i].clone();
}
separateRows[0][0] = 4;
System.out.println("Original after row copy: " + rows[0][0]);
System.out.println("Separate row value: " + separateRows[0][0]);
}
}With the Java Development Kit installed, compile and run it:
javac ArrayCopyDemo.java
java ArrayCopyDemoThe score lines show 7, 9, and 0. Assigning scores to same did not make a copy, so the original score became 7. Changing longer did not change scores, and the extra integer element is 0.
What “shallow” means
A shallow copy creates a new array but copies each element as it is.
- For
int[], the elements are integer values. Each value is copied. - For
Box[], the elements are references. Each reference is copied, but theBoxobjects are not.
That is why changing the box through boxCopy makes the original box value print as 6. Both arrays point to the same box. Replacing boxCopy[0] with a new Box, however, would not replace boxes[0].
The same issue appears with an array of arrays. Each element of rows points to a row array. Cloning only rows leaves those row arrays shared, so the original row value becomes 8.
The loop then copies each non-null row. Changing separateRows[0][0] to 4 leaves the original value at 8. For arrays of objects, a fully independent copy would also need new objects for the elements. If those objects contain other shared objects, those may need copying too.
Exercise
If you clone a Box array and change a shared Box’s value through the clone, does the original array see the change? Why?
Show answer
Yes. The clone is a new array, but its copied reference points to the same Box.