The Arrays.copyOf() method in Java creates a new array by copying elements from an existing array, with the option to change the size of the new array. It is commonly used to duplicate or resize arrays safely and efficiently.
- Creates a new array by copying all or a specified number of elements from the original array.
- Supports resizing arrays by truncating extra elements or filling additional positions with default values.
import java.util.Arrays;
public class ArraysCopyOf {
public static void main(String[] args)
{
int[] arr1 = {1, 2, 3, 4, 5};
// copy the array to a new array with the same length
int[] arr2 = Arrays.copyOf(arr1, arr1.length);
System.out.println("Original Array: " + Arrays.toString(arr1));
System.out.println("Copied Array: " + Arrays.toString(arr2));
}
}
Output
Original Array: [1, 2, 3, 4, 5] Copied Array: [1, 2, 3, 4, 5]
Explanation: This demonstrates how Arrays.copyOf() method can be used to duplicate a 1D array with basic functionality before exploring complex examples.
Syntax
public static datatype[] copyOf(datatype[] original, int newLength)
Parameters
- original: The original array to be copied.
- newLength: The length of the new array.
Returns
- Returns a new array containing the copied elements from the original array.
- If newLength is smaller than the original array length, the array is truncated.
- If newLength is larger than the original array length, the remaining elements are filled with default values (such as 0, false, or null, depending on the array type).
Copying 1D Arrays
Here, we will learn how to use Arrays.copyOf() method to copy a 1D array and then modify its extra elements.
- Creates a new one-dimensional array containing the elements of the original array.
- Allows the copied array to have the same, smaller, or larger length than the source array.
- Extra elements can be updated independently after copying without changing the original array.
Syntax:
Arrays.copyOf(int [] original, int newLength);
- original: The original array to be copied.
- newLength: The desired length of the new copy.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// initialize original array
int[] arr1 = new int[] {1, 2, 3};
System.out.println("Original Array:");
for (int i = 0; i < arr1.length; i++)
System.out.print(arr1[i] + " ");
// Copying array 'arr1' to 'arr2'
// with specified size
int[] arr2 = Arrays.copyOf(arr1, 5);
// Modifying the newly added elements
arr2[3] = 4;
arr2[4] = 5;
// Displaying the copied
// array after modifications
System.out.println("\n\nCopied array after modifications:");
for (int i = 0; i < arr2.length; i++)
System.out.print(arr2[i] + " ");
}
}
Output
Original Array: 1 2 3 Copied array after modifications: 1 2 3 4 5
Copying 1D Array with a Larger Length
When the specified length is larger than the original array, Arrays.copyOf() creates a bigger array and copies all existing elements into it. The remaining positions are automatically initialized with the default values of the array's data type.
- Allocates additional positions beyond the original array size.
- Default values depend on the array type, such as 0 for numeric types, false for boolean, and null for object references.
import java.util.Arrays;
public class Main {
public static void main(String args[]) {
// initializing an array original
int[] arr1 = new int[] {1, 2 ,3};
System.out.println("Original Array:");
for (int i = 0; i < arr1.length; i++)
System.out.print(arr1[i] + " ");
// copying array arr1 to arr2
// Here, new array has 5 elements
int[] arr2 = Arrays.copyOf(arr1, 5);
System.out.print("\nNew array of higher length:\n");
for (int i = 0; i < arr2.length; i++)
System.out.print(arr2[i] + " ");
}
}
Output
Original Array: 1 2 3 New array of higher length: 1 2 3 0 0
Copying 2D Arrays
When copying a two-dimensional array, the Arrays.copyOf() method creates a shallow copy of the outer array. As a result, the row references are copied, but the individual row arrays are shared between the original and copied arrays.
- Copies only the outer array, while the inner arrays remain shared between both arrays.
- Changes made to elements inside a shared row are reflected in both the original and copied arrays.
Syntax
Arrays.copyOf(originalArray, newLength);
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// Original 2D array
int[][] arr1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Copying the 2D array
int[][] arr2 = copy2DArray(arr1);
System.out.println("Original Array:");
print2DArray(arr1);
System.out.println("Copied Array:");
print2DArray(arr2);
}
// Method to copy a 2D array
public static int[][] copy2DArray(int[][] arr1) {
// Create a new 2D array with the same
// number of rows as the original
int[][] arr2 = new int[arr1.length][];
// Copy each row using Arrays.copyOf() method
for (int i = 0; i < arr1.length; i++) {
arr2[i] = Arrays.copyOf(arr1[i], arr1[i].length);
}
return arr2;
}
// Method to print a 2D array
public static void print2DArray(int[][] arr3) {
for (int[] r : arr3) {
System.out.println(Arrays.toString(r));
}
}
}
Output
Original Array: [1, 2, 3] [4, 5, 6] [7, 8, 9] Copied Array: [1, 2, 3] [4, 5, 6] [7, 8, 9]