Copying and Filling Arrays in Java
- Details
- Written by Nam Ha Minh
- Last Updated on 11 April 2024   |   Print Email
Besides sorting and searching, the java.util.Arrays class provides also convenient methods for copying and filling arrays. In this article, we’re going to help you understand these functionalities in details with full code examples.
1. Copying Arrays Example
The Arrays class provides convenient methods that return a new array which is a copy of a specified array. The newly returned array can have same, less or more elements than the original array. If have more, the new array is padded with default values for the extra elements. Below is the list of such methods (X denotes a primitive type; U and X denote reference types):
- X[] copyOf(X[] original, int newLength)
- T[] copyOf(T[] original, int newLength)
- T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType)
And the following methods copy the specified range of the specified array into a new array:
- X[] copyOfRange(X[] original, int from, int to)
- T[] copyOfRange(T[] original, int from, int to)
- T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType)
Behind the scenes, these methods use the System.arraycopy() method.
Let’s see some examples.
The following example illustrates how to copy primitive arrays:
int[] original = {199, 299, 399, 499, 599, 699, 799, 899, 999}; int[] copy1 = Arrays.copyOf(original, original.length); int[] copy2 = Arrays.copyOf(original, 5); int[] copy3 = Arrays.copyOf(original, 15); System.out.println("original array: " + Arrays.toString(original)); System.out.println("copy #1 (same length): " + Arrays.toString(copy1)); System.out.println("copy #2 (truncted): " + Arrays.toString(copy2)); System.out.println("copy #3 (padded): " + Arrays.toString(copy3));
Output:
original array: [199, 299, 399, 499, 599, 699, 799, 899, 999] copy #1 (same length): [199, 299, 399, 499, 599, 699, 799, 899, 999] copy #2 (truncated): [199, 299, 399, 499, 599] copy #3 (padded): [199, 299, 399, 499, 599, 699, 799, 899, 999, 0, 0, 0, 0, 0, 0]
Note that the padded elements are initialized to zeroes (for primitive numbers).
Similarly, the following code snippet shows how to copy an array of String objects:
String[] original = {"Apple", "Banana", "Carrot", "Lemon", "Orange", "Grape"}; String[] copy1 = Arrays.copyOf(original, original.length); String[] copy2 = Arrays.copyOf(original, 3); String[] copy3 = Arrays.copyOf(original, 10); System.out.println("original array: " + Arrays.toString(original)); System.out.println("copy #1 (same length): " + Arrays.toString(copy1)); System.out.println("copy #2 (truncated): " + Arrays.toString(copy2)); System.out.println("copy #3 (padded): " + Arrays.toString(copy3));
Output:
original array: [Apple, Banana, Carrot, Lemon, Orange, Grape] copy #1 (same length): [Apple, Banana, Carrot, Lemon, Orange, Grape] copy #2 (truncated): [Apple, Banana, Carrot] copy #3 (padded): [Apple, Banana, Carrot, Lemon, Orange, Grape, null, null, null, null]
Here, note that the padded elements are initialized to nulls (for object reference types).
The following example shows how to copy an array of Integer to an array of Number (Number is the super type of Integer):
Integer[] integers = {16, 32, 64, 128, 256, 512}; Number[] numbers = Arrays.copyOf(integers, integers.length, Number[].class); System.out.println("Numbers: " + Arrays.toString(numbers));
Output:
Numbers: [16, 32, 64, 128, 256, 512]
And the following example illustrates how to copy a specified range of an array into a new array:
int[] original = {8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096}; int[] copy = Arrays.copyOfRange(original, 3, 8); System.out.println("Original: " + Arrays.toString(original)); System.out.println("Sub copy: " + Arrays.toString(copy));
Output:
Original: [8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096] Sub copy: [64, 128, 256, 512, 1024]
2. Filling Arrays Example
Sometimes you need to assign the same value for all elements of an array (or “reset” the entire array with a value). The Arrays class provides such methods (X denotes a primitive type):
- void fill(X[] a, X val)
- void fill(X[] a, int fromIndex, int toIndex, X val)
- void fill(Object[] a, Object val)
- void fill(Object[] a, int fromIndex, int toIndex, Object val)
Let’s see some examples.
The following code fills zeroes to an array of integers:
int[] numbers = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21}; System.out.println("Before fill: " + Arrays.toString(numbers)); Arrays.fill(numbers, 0); System.out.println("After fill: " + Arrays.toString(numbers));
Output:
Before fill: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21] After fill: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
The following code “resets” Strings in an array to null:
String[] fruits = {"Banana", "Apple", "Orange", "Lemon", "Mango"}; System.out.println("Before fill: " + Arrays.toString(fruits)); Arrays.fill(fruits, null); System.out.println("After fill: " + Arrays.toString(fruits));
Output:
Before fill: [Banana, Apple, Orange, Lemon, Mango] After fill: [null, null, null, null, null]
And the following code snippet fills only a half of an array:
double[] doubles = {1.23, 2.34, 3.45, 4.56, 5.67, 6.78, 7.89, 8.90}; System.out.println("Before fill: " + Arrays.toString(doubles)); Arrays.fill(doubles, 0, doubles.length / 2, 0.0); System.out.println("After fill: " + Arrays.toString(doubles));
Output:
Before fill: [1.23, 2.34, 3.45, 4.56, 5.67, 6.78, 7.89, 8.9] After fill: [0.0, 0.0, 0.0, 0.0, 5.67, 6.78, 7.89, 8.9]
3. Array to String
As you can see in the examples throughout this article, the Arrays class provides convenient methods for representing elements of an array in String format (X denotes a primitive type):
- String toString(X[] a)
- String toString(Object[] a)
However, these methods work only with 1-dimension arrays. For multi-dimensional arrays (array of arrays), use the following method:
String deepToString(Object[] a)
Here’s an example that prints content of a 2-dimenson array:
String[][] persons = { {"Tom", "USA", "Developer", "Jogging"}, {"John", "Canada", "Designer", "Painting"}, {"Alice", "UK", "Tester", "Biking"} }; System.out.println(Arrays.deepToString(persons));
Output:
[[Tom, USA, Developer, Jogging], [John, Canada, Designer, Painting], [Alice, UK, Tester, Biking]]
References:
Related Java Arrays Tutorials:
Other Java Collections Tutorials:
- What is Java Collections Framework?
- Java Queue Tutorial
- Java List Tutorial
- Java Set Tutorial
- Java Map Tutorial
- Understand equals and hashCode
- Understand object ordering
Comments