Java Arrays Basics
The standard array-processing methods are static methods defined in the Arrays class, which is in the java.util package.
A Copying Problem Java Already Solved
Suppose you have an array and you need a second array with the same contents. You could write a loop that creates a new array of the same size and copies each element across, one index at a time. That works, but it is repetitive, and every programmer who needs a copy would write the same loop again. Java anticipates this need. It ships with a set of standard array-processing methods so that common jobs like copying do not have to be hand-written each time.
This article works through three practical questions: where these methods live, how you call them, and what you should expect when you use them. Copying is the example used to introduce the whole group, so keep the copying problem in mind as you read. By the end you should be able to predict what happens to an original array after you change its copy.
Where the Array Methods Live
The standard array-processing methods are static methods defined in the Arrays class, which is in the java.util package.
Notice the naming pattern. The method belongs to the class Arrays, not to an individual array variable. Whether you are working with a small array of numbers or a large one, the method is reached through the same class name. The array you want to process is passed in as an argument. Because the methods are static, you call them on the class itself, so you write Arrays followed by a dot and the method name.
The class lives in java.util, so a program that uses it needs an import statement that brings Arrays into scope. The example below shows the import, the array, and one call to the copying method, Arrays.copyOf. The source uses this method as its main example of a standard helper: you name the array you want, and the method produces the copy for you.
import java.util.Arrays;
public class CopyDemo {
public static void main(String[] args) {
int[] original = {1, 2, 3};
int[] copy = Arrays.copyOf(original, original.length);
System.out.println(Arrays.toString(copy));
}
}- 1The import statement names the class and its package.
- 2The array original is created with three elements.
- 3
Arrays.copyOf(original, original.length)asks the Arrays class to produce a copy of original with the same length, and the result is stored in copy. - 4
Arrays.toString(copy)formats the copy's contents for printing, so the output shows the three values.
- import
java.util.Arraysbrings the class from thejava.utilpackage into scope Arrays.copyOfis a static method, so it is called on the class name- original is passed as the first argument;
original.lengthgives the requested length of the copy
[1, 2, 3]Tracing a Copy as a Separate Array
The key fact about a copying method is that its result is a separate array. The original is not the thing that gets returned; the new array is. The diagram below shows the relationship. The original array is handed to Arrays.copyOf, and the method returns a second array that holds the same values. The two arrays are distinct objects, so each one can be changed without affecting the other.
Read the diagram from left to right. The original array goes in, and the separate copy comes out. Each array is its own container of values, so the copy is not a view onto the original. This is the reason copying is useful: once the copy exists, you can work with it freely.
int[] original = {1, 2, 3};
int[] copy = Arrays.copyOf(original, original.length);original holds 1, 2, 3 and copy holds 1, 2, 3 as a separate array
int[] original = {1, 2, 3};
int[] copy = Arrays.copyOf(original, original.length);
copy[0] = 99;copy holds 99, 2, 3 while original still holds 1, 2, 3
What do you think happens?
After copy[0] = 99 runs, what does original[0] print?
Reveal answer
1
The copy is a separate array. Writing to copy[0] changes only the copy, so original[0] keeps its value of 1.
One Name, Many Array Types
The source makes a point that is easy to skim past: the methods in the Arrays class, copyOf included, come in multiple versions, and each version is meant for a different array type. So Arrays.copyOf is not a single method that handles every kind of array in exactly one way. It is a family of methods that share a name, with each member written for one kind of array.
This is the practical reason the same name works on arrays of different element types. When you call the method, Java selects the version that matches the array you pass in. You do not need to remember a separate name for each array type. What you do need to check is that the array you pass in is the type you intend to copy, because that type determines which version runs.
Choosing the Length of the Copy
Arrays.copyOf takes a length as its second argument, and that length does not have to match the original. In the example below, the requested length is 5 while the original has 3 elements. Java copies the original values into the first positions of the new array, and fills the remaining positions with the default value for the element type. For an int array, that default is 0. When the requested length is shorter than the original, the copy keeps only as many leading elements as fit.
[1, 2, 3, 0, 0]
[1, 2]- A requested length of 5 pads the copy with default 0 values
- A requested length of 2 keeps only the first two elements
Mistakes When Copying Arrays
Common mistake Assuming the copy is linked to the original
What it looks likeAfter copy[0] = 99, expecting original[0] to also become 99
Why it failsThe method returns a separate array. Each array holds its own values.
Do this insteadTreat the copy as independent, and change the original explicitly if you need both to change.
Common mistake Writing a separate method name for each array type
What it looks likeLooking for a method called copyOfInt when the array is an int array
Why it failsThe Arrays class uses one name with multiple versions, one per array type.
Do this insteadCall Arrays.copyOf and make sure the array you pass is the type you intend to copy.
Common mistake Expecting the requested length to be ignored
What it looks likeCalling Arrays.copyOf(original, 5) on a 3-element array and expecting exactly 3 elements
Why it failsThe second argument sets the length of the new array, so it can be longer or shorter than the original.
Do this insteadPass the length you want. Use original.length when you want an exact duplicate.
Practice: Copy and Compare
Write a short program that creates an int array with the values 4, 5, 6. Use Arrays.copyOf to make a copy with the same length, change the first element of the copy to 40, and print both arrays. Before you run the program, predict what the original array will show for its first element.
Hints
- Remember the import statement for
java.util.Arrays - The copy is a separate array, so the change should not reach the original
- Use
Arrays.toStringto print each array's contents
Key Takeaways
- The standard array-processing methods are static methods of the Arrays class, which is in the
java.utilpackage, so you call them through the class name and pass the array as an argument. Arrays.copyOfreturns a separate array, so changing the copy does not change the original.- Methods in the Arrays class come in multiple versions, one for each array type, so one method name works across different array types and Java picks the matching version.
- The length argument to
Arrays.copyOfcan be longer or shorter than the original; longer copies are padded with default values, and shorter copies keep only the leading elements.