Signup/Sign In

Java Arrays copyOf() Method

In this tutorial, we will learn about copyOf() method in Java. This method belongs to the Arrays class which is located in java.util package. It is used to create a new array from an existing array or copying one array to another.

Syntax

This is the syntax for copyOf() method. This method receives an existing array with the length of a new array and returns a new array with the values from an old array.

public static datatype[] copyOf(datatype[] arr,int newLength)

List of Overloading Methods

This table contains all the overloaded variants of copyOf() method of Arrays class.

Method

Description

static boolean[] copyOf(boolean[] original, int newLength)

This method copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length.

static byte[] copyOf(byte[] original, int newLength)

This method copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.

static char[] copyOf(char[] original, int newLength)

This method copies the specified array, truncating or padding with null characters (if necessary) so the copy has the specified length.

static double[] copyOf(double[] original, int newLength)

This method copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.

static float[] copyOf(float[] original, int newLength)

This method copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.

static int[] copyOf(int[] original, int newLength)

This method copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.

static long[] copyOf(long[] original, int newLength)

This method copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.

static short[] copyOf(short[] original, int newLength)

This method copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.

static <T> T[] copyOf(T[] original, int newLength)

This method copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length.

static <T,?U> T[] copyOf(U[] original,

int newLength, Class<? extends T[]> newType)

This method copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length.

Example to implement copyOf() method

In the below example, we are creating a new array new_arry from an existing array arr of length 4. From the output of this program, we can compare both the arrays. All four elements from an array copy to a new array as it is. It indicates that we can create a new array of different sizes from an existing array using copyOf() method.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		int[] arr = new int[] {5, 6, 7, 10, 17};

		System.out.println("Original Array"); 
		for(int num:arr)
		{
			System.out.print(num+" ");
		} 

		int[] new_arr = Arrays.copyOf(arr, 4); 

		System.out.println("\nNew Array"); 
		for(int num:new_arr)
		{
			System.out.print(num+" ");
		}
	}
}


Original Array
5 6 7 10 17
New Array
5 6 7 10

Example: Copy Array into Larger Array

In the code given below, we can clearly see the array length of an existing array is 5, and the length of a new array is 10 it means the new array is short of 5 values for new array elements. In such a case remaining elements will be assigned as a 0.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		int[] arr = new int[] {5, 6, 7, 10, 17};

		System.out.println("Original Array"); 
		for(int num:arr)
		{
			System.out.print(num+" ");
		} 

		int[] new_arr = Arrays.copyOf(arr, 10); 

		System.out.println("\nNew Array"); 
		for(int num:new_arr)
		{
			System.out.print(num+" ");
		}
	}
}


Original Array
5 6 7 10 17
New Array
5 6 7 10 17 0 0 0 0 0

Example: Copy Array of different Types

In this example, we are implementing all other variants of copy() method to check how they work with other

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		//Example of static boolean[] copyOf(boolean[] original, int newLength)
		boolean[] array1={true, false, true, false, false, true};
		boolean[] new_arr1=Arrays.copyOf(array1, 6);
		System.out.println("New Boolean Array: "+Arrays.toString(new_arr1));

		//Example of static byte[]	copyOf(byte[] original, int newLength)
		byte[] array2={4, 1, 8, 2, 2, 3, 16, 4, 4};
		byte[] new_arr2=Arrays.copyOf(array2, 7);
		System.out.println(Arrays.toString(new_arr2));

		//Example of static char[]	copyOf(char[] original, int newLength)
		char[] array3={'a', 'e', 'i', 'o', 'u'};
		char[] new_arr3=Arrays.copyOf(array3, 10);
		System.out.println(Arrays.toString(new_arr3));

		//Example of static double[] copyOf(double[] original, int newLength)
		double[] array4={12, 3, 17, 5, 8, 17, 2, 37};
		double[] new_arr4=Arrays.copyOf(array4, 10);
		System.out.println(Arrays.toString(new_arr4));

		//Example of static float[]	copyOf(float[] original, int newLength)
		float[] array5={4.12f, 7.3f, 8.17f, 1.5f, 7.8f, 4.17f, 3.2f, 6.37f};
		float[] new_arr5=Arrays.copyOf(array5, 7);
		System.out.println(Arrays.toString(new_arr5));

		//Example of static int[]	copyOf(int[] original, int newLength)
		int[] array6 ={5, 6, 7, 10, 17};		
		int[] new_arr6=Arrays.copyOf(array6, 5);
		System.out.println(Arrays.toString(new_arr6));

		//Example of static long[]	copyOf(long[] original, int newLength)
		long[] array7={4,54,56,584,51,84,5,28,33,9};
		long[] new_arr7=Arrays.copyOf(array7, 5);
		System.out.println(Arrays.toString(new_arr7));

		//Example of static short[]	copyOf(short[] original, int newLength)
		short[] array8={10, 8, 6, 4, 2, 0};
		short[] new_arr8=Arrays.copyOf(array8, 5);
		System.out.println(Arrays.toString(new_arr8));
	}
}


New Boolean Array: [true, false, true, false, false, true]
[4, 1, 8, 2, 2, 3, 16]
[a, e, i, o, u]
[12.0, 3.0, 17.0, 5.0, 8.0, 17.0, 2.0, 37.0, 0.0, 0.0]
[4.12, 7.3, 8.17, 1.5, 7.8, 4.17, 3.2]
[5, 6, 7, 10, 17]
[4, 54, 56, 584, 51]
[10, 8, 6, 4, 2]

Conclusion:

In this tutorial, we learned how copyOf() method can be used to create a new array using an existing array. New array elements will be assigned from an existing value from an old array. If the length of the new array is greater than the old array in such cases all remaining element s will be assigned as zero.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.