Java Arrays copyOf()

java.util.Arrays.copyOf() method copies specified array into a new array with specified length.

If length of new array is greater than that of source array, copyOf() method copies the whole source array to new array and pads the new array with zeros, at end, to match the specified length of new array.

If the length of new array is less than that of source array, copyOf() method truncates the source array to the specified length and copies to new array.

There are many variations of copyOf() method in java.util.Arrays class to support different datatypes of arrays.

Following are the declarations of copyOf() method supporting different builtin datatypes, and custom class types.

public static boolean[] copyOf(boolean[] original, int newLength)
public static double[] copyOf(double[] original, int newLength)
public static float[] copyOf(float[] original, int newLength)
public static char[] copyOf(char[] original, int newLength)
public static long[] copyOf(long[] original, int newLength)
public static int[] copyOf(int[] original, int newLength)
public static short[] copyOf(short[] original, int newLength)
public static byte[] copyOf(byte[] original, int newLength)
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType)
public static <T> T[] copyOf(T[] original, int newLength)

In this tutorial, we will go through different examples based on the type of array we try to copy, and new length being more or less than that of the original array.

The working of copyOf() method for different datatypes should remain same, except for the default value that is padded to new array when new length is greater than length of original array. In this specific case, the default value that is padded to new array depends on the datatype. The default value for int would be 0, string would be null, etc.

Example 1 – java.util.Arrays.copyOf() – integer array

We will learn how to copy an integer array using Arrays.copyOf() method.

In the following example, we have original array of size 4. We will copy this array to a new array with new length of 2. As new length is less than length of original array, copyOf() truncates the original array to new length, which is 2, and copies to new array.

Java Program

import java.util.Arrays;

public class Example {
	public static void main(String[] args) {
		int originalArray[] = {25, 87, 63, 91};
		int newLength = 2;
		int newArray[] = Arrays.copyOf(originalArray, newLength);
		
		for(int x: newArray)
			System.out.print(x+" ");
	}
}

Output

25 87

Now, we will go through a scenario, where new length is greater than the length of original array.

In the following example, we have original array of size 4. We will copy this array to a new array with new length of 7. As new length is greater than length of original array, copyOf() pads zeroes to the new array after copying original array to new array.

Java Program

import java.util.Arrays;

public class Example {
	public static void main(String[] args) {
		int originalArray[] = {25, 87, 63, 91};
		int newLength = 7;
		int newArray[] = Arrays.copyOf(originalArray, newLength);
		
		for(int x: newArray)
			System.out.print(x+" ");
	}
}

Output

25 87 63 91 0 0 0

Sometimes, we may need to copy the original array to new array as is. In other words, new length should be same as that of original array.

In the following program, we will copy original array as is to the new array. For this we will assign the new length with the length of original array.

Java Program

import java.util.Arrays;

public class Example {
	public static void main(String[] args) {
		int originalArray[] = {25, 87, 63, 91};
		int newLength = originalArray.length;
		int newArray[] = Arrays.copyOf(originalArray, newLength);
		
		for(int x: newArray)
			System.out.print(x+" ");
	}
}

Output

25 87 63 91
ADVERTISEMENT

Example 2 – java.util.Arrays.copyOf() – String array

In the above examples, we copied an integer array using copyOf() method.

As already mentioned, working of copyOf() method for other datatypes also should remain same. Let us write a Java program for the case when new length is more that that of original array.

Java Program

import java.util.Arrays;

public class Example {
	public static void main(String[] args) {
		String originalArray[] = {"apple", "banana", "cherry"};
		int newLength = 5;
		String newArray[] = Arrays.copyOf(originalArray, newLength);
		
		for(String x: newArray)
			System.out.print(x+" ");
	}
}

Output

apple banana cherry null null

Conclusion

In this Java Tutorial, we learned how to use Arrays.asList() method of java.util package, to create a List from objects or an array, with the help of example programs.