Arrays copyOf() in Java with Examples
Last Updated :
11 Nov, 2024
Arrays.copyOf() is a method of java.util.Arrays class. It is used to copy the specified array, truncating or padding with false (for boolean arrays) if necessary so that the copy has the specified length.
This method can be used with both 1D and 2D arrays, but it’s important to note that Arrays.copyOf() performs a shallow copy, meaning that if you’re copying an array of objects, the objects themselves are not duplicated—only the references to them.
Example:
The below example shows the basic use of Arrays.copyOf() method to create a copy of an array with a specified length.
Java
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));
}
}
OutputOriginal Array: [1, 2, 3, 4, 5]
Copied Array: [1, 2, 3, 4, 5]
This demonstrates how Arrays.copyOf() method can be used to duplicate a 1D array with basic functionality before exploring complex examples.
Copying 1D Arrays
Here, we will learn how to use Arrays.copyOf()
method to copy a 1D array and then modify its extra elements.
Syntax to Copy 1D Array
Arrays.copyOf(int [] original, int newLength);
- original: The original array to be copied.
- newLength: The desired length of the new copy.
Example of Copying 1D Array
Java
// Java program to illustrate copyOf()
// method for 1D arrays
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] + " ");
}
}
OutputOriginal Array:
1 2 3
Copied array after modifications:
1 2 3 4 5
Copying 1D Array with a Larger Length
When the length of the copied array is greater than the original, the new array is padded with default values (0 for int, false for boolean, and null for reference types) to fill the remaining indices.
Example:
Java
// Java program to illustrate the use of copyOf
// when new array is of higher length
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] + " ");
}
}
OutputOriginal Array:
1 2 3
New array of higher length:
1 2 3 0 0
Copying 2D Arrays
When working with 2D arrays, the Arrays.copyOf()
method performs a shallow copy, meaning only the references to each row are copied. For a deep copy of the rows in a 2D array, you would need to copy each row individually.
Syntax to Copy 2D Array
Arrays.copyOf(originalArray, newLength);
Example of Copying 2D Array
Java
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));
}
}
}
OutputOriginal Array:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Copied Array:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]