Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

Java Arrays.copyOf Method

The Java Arrays.copyOf Method is one of the Java Array Methods, which is to copy the array content into a new array of user-specified length. In this article, we will show how to copy Java Array to a new array with examples. The basic syntax of the Arrays.copyOf in Java Programming language is as shown below.

Java Arrays.copyOf Method syntax

The Java Programming Language provides nine different Java Arrays copyof methods to copy the specified Java Array to New Array. While coping Array:

  • If the specified user length is greater than the Original Array, then the remaining elements will be filled with default values of the data type. For example, array a[5] of integer type holds five elements (1,2 3, 4, 5), but we specified the new length as 7 (Arrays.copyOf(b, 7), then b array will become b[7] holding elements 1, 2, 3, 4, 5, 0, 0 (Here, 0 is the default value for int type)
  • If the specified user length is less than the Original Array, then the remaining elements will be truncated. For example, array a[7] of integer type holds five elements (10, 15, 22, 45, 65, 3, 54), but we specified the new length as 4 (Arrays.copyOf(b, 4), then b array will become b[4] holding elements 10, 15, 22, 45.

The following Java arrays copyof method will accept the Boolean Array and Array length as the parameters and copies the Java Boolean array to a new array of user-specified length.

public static boolean copyOf(Boolean[] anBooleanArray, int newLength); // It will return Boolean Array

//In order to use in program
Arrays.copyOf(Boolean[] anBooleanArray, int newLength);

The following Java arrays copyof method will accept the Byte Array and Array length as the parameters and copies the Java Byte array to a new byte array of user-specified length.

public static byte copyOf(byte[] anByteArray, int newLength); // It will return Byte Array

//In order to use in program
Arrays.copyOf(byte[] anByteArray, int newLength);

This Java array copyof method will accept the short Array and Array length as the parameters and copies the Java short array to a new short array of user-specified length.

public static short copyOf(short[] anShortArray, int newLength); // It will return short Array

//In order to use in program
Arrays.copyOf(short[] anShortArray, int newLength);

It accepts the Character Array and Array length as the parameters and copies the Java char array to a new Character array of user-specified length.

public static char copyOf(char[] anCharArray, int newLength); // It will return char Array

//In order to use in program
Arrays.copyOf(char[] anCharArray, int newLength);

The following method will accept the Integer Array and Array length as the parameters and copies the Java Integer array to a new Integer array of user-specified length.

public static short copyOf(int[] anIntArray, int newLength); // It will return Integer Array

//In order to use in program
Arrays.copyOf(int[] anIntArray, int newLength);

This Java array copyof method will accept the Long Array and Array length as the parameters and copies the Java Long array to a new Long array of user-specified length.

public static long copyOf(long[] anLongArray, int newLength); // It will return Long Array

//In order to use in program
Arrays.copyOf(long[] anLongArray, int newLength);

It accepts the Double Array and Array length as the parameters. It copies the Java Double array to a new Double array of user-specified length.

public static double copyOf(double[] anDoubleArray, int newLength); // It will return Double Array

//In order to use in program
Arrays.copyOf(double[] anDoubleArray, int newLength);

Accept the Float Array and Array length and copies the Java Float array to a new Float array of given length.

public static float copyOf(float[] anFloatArray, int newLength); // It will return float Array

//In order to use in program
Arrays.copyOf(float[] anFloatArray, int newLength);

This Java array copyof method accepts the Object Array and Array length as the parameters and copies the Java Object array to new Object array of user-specified length.

public static <T> T[] copyOf(T[] Array, int newLength); 

//In order to use in program
Arrays.copyOf(T[] Array, int newLength);

It accepts the Object Array and Array length as the parameters. It copies the Java Object array to a new array of user-specified length and Object type.

public static <T,U> T[] copyOf(U[] Array, int newLength, Class<? extends T[]> newType); 

//In order to use in program
Arrays.copyOf(T[] Array, int newLength, Class<? extends T[]> newType);
  • Array: This is the Original Array. We are going to use the Arrays.copyOf method to copy this array to a new array.
  • newlength: Please specify the length of the new array. Based on the length, array elements truncated or extended (with default values)
  • newType: Please specify the class type you want to apply for your new array.

Java Arrays.copyOf to copy Byte Array

In this Java program, we declared the byte array with random array elements. Then we call public static byte copyOf (byte[] anByteArray, int newLength) method to copy the Java Byte array to a new array of a specified length.

// Java Example to Copy Byte Array to New Array
package ArrayMethods;
import java.util.Arrays;
public class CopyOfBytes {
	public static void main(String[] args) {
		byte[] byteArray = {12, 24, 26}; 		
		//Copying Array
		byte[] bitArray = Arrays.copyOf(byteArray, 5);
		System.out.println("New Byte Array after Copying:");
		arrayPrint(bitArray);
		
		//Copying Array and assign Values
		byte[] btArray = Arrays.copyOf(byteArray, 5);
		btArray[3] = 5;
		btArray[4] = 19;
		System.out.println("New Byte Array with Vlaues:");
		arrayPrint(btArray);
	}
	public static void arrayPrint(byte[] anByteArray) {
		for (byte Number: anByteArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Java Arrays.copyOf Method 1

In this Java array copyof method example, we declared a byte Array and assigned some random values as the array elements using the following statement.

byte[] byteArray = {12, 24, 26};

Next, we used the Arrays.copyOf method to copy the above-specified byte array to bitArray of length 5. It will copy the available elements from byteArray to bitArray and fill the remaining values with a default value.

byte[] bitArray = Arrays.copyOf(byteArray, 5);

The following Java statement is to print the Byte array elements to the output.

arrayPrint(byteArray);

Next line, we used the Java Arrays.copyOf method to copy the above specified byte array to btArray of length 5, and then we are assigning the new values to index position 3 and 4. It means these values will replace default values (zeros)

byte[] btArray = Arrays.copyOf(byteArray, 5);
btArray[3] = 5;
btArray[4] = 19;

The following statement is to print the Byte Array elements to the output.

arrayPrint(byteArray);

When the compiler reaches the above statement, the compiler will jump to the following function. From the below code snippet, we used the Java Foreach Loop to iterate the Byte Array. Then we are printing every array element using the System.out.println statement.

public static void arrayPrint(byte[] anByteArray) {
	for (byte Number: anByteArray) {
		System.out.println("Array Elelment = " + Number);
	}
}

Arrays.copyOf to copy Boolean Array

In this Java program, we declared the Boolean array with random array elements. Then we call public static Boolean copyOf (boolean[] anBooleanArray, int newLength) method to copy the Java Boolean array to a new array of a specified length.

// Java Example to Copy Boolean Array to New Array
package ArrayMethods;
import java.util.Arrays;

public class CopyOfBoolean {
	public static void main(String[] args) {
		boolean[] bool = {true, true};
		boolean[] boolArray =  Arrays.copyOf(bool, 4);
		System.out.println("New Boolean Array after Copying:");
		arrayPrint(boolArray);
		
		boolean[] bolArray =  Arrays.copyOf(bool, 4);
		bolArray[2] = false;
		bolArray[3] = true;
		System.out.println("New Boolean Array with Vlaues:");
		arrayPrint(bolArray);
	}
	public static void arrayPrint(boolean[] anBooleanArray) {
		for (boolean bool: anBooleanArray) {
			System.out.println("Array Elelment = " + bool);
		}
	}
}
Java Arrays.copyOf Method 2

Java Arrays.copyOf to copy Short Array

In this Java program, we declared the short array with random array elements. Then we call public static Short copyOf (short[] anShortArray, int newLength) method to copy the short array to a new array of a specified length.

// Java Example to Copy Short Array to New Array
package ArrayMethods;
import java.util.Arrays;
public class ShortCopyOf {
	public static void main(String[] args) {
		short[] srtArray = {2, 6, 4}; 		
		short[] ShortArray = Arrays.copyOf(srtArray, 5);
		System.out.println("New Short Array after Copying:");
		arrayPrint(ShortArray);
		
		short[] ShrtArray = Arrays.copyOf(srtArray, 5);
		ShrtArray[3] = 3;
		ShrtArray[4] = 9;
		System.out.println("New Short Array with Vlaues:");
		arrayPrint(ShrtArray);
	}
	public static void arrayPrint(short[] anShortArray) {
		for (short Number: anShortArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Java Arrays.copyOf Method 3

Arrays.copyOf to copy Integer Array

In this Java program, we declared the integer array with random array elements. Then we call public static IntcopyOf (int[] anIntegerArray, int newLength) method to copy the Java Int array to a new array of a specified length.

// Java Example to Copy Integer Array to New Array
package ArrayMethods;
import java.util.Arrays;
public class CopyOfIntegers {
	public static void main(String[] args) {
		int[] intArray = {25, 45}; 		
		int[] integerArray = Arrays.copyOf(intArray, 5);
		System.out.println("New Integer Array after Copying:");
		arrayPrint(integerArray);
		
		int[] anIntArray = Arrays.copyOf(intArray, 5);
		anIntArray[2] = 65;
		anIntArray[4] = 75;
		System.out.println("New Integer Array with Vlaues:");
		arrayPrint(anIntArray);
	}
	public static void arrayPrint(int[] anIntArray) {
		for (int Number: anIntArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Java Arrays.copyOf Method 4

Java Arrays.copyOf to copy Long Array

In this Java program, we declared the long array with random array elements. Then we call the public static LongcopyOf (long[] anLongArray, int newLength) method to copy the Java Long array to a new array of a specified length.

// Java Example to Copy Long Array to New Array
package ArrayMethods;
import java.util.Arrays;

public class CopyOfLong {
	public static void main(String[] args) {
		long[] LngArray = {504, 405}; 		
		long[] longArray = Arrays.copyOf(LngArray, 5);
		System.out.println("New Long Array after Copying:");
		arrayPrint(longArray);
		
		long[] anLongArray = Arrays.copyOf(LngArray, 5);
		anLongArray[2] = 265;
		anLongArray[4] = 750;
		System.out.println("New Long Array with Vlaues:");
		arrayPrint(anLongArray);
	}
	public static void arrayPrint(long[] anLongArray) {
		for (long Number: anLongArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Java Arrays.copyOf Method 5

Arrays.copyOf to copy Double Array

In this Java arrays copyof program, we declared the double array with random array elements. Next, we call the public static Double copyOf (double[] anDoubleArray, int newLength) to copy the Java double array to a new array.

// Java Example to Copy Double Array to New Array
package ArrayMethods;
import java.util.Arrays;

public class CopyOfDouble {
	public static void main(String[] args) {
		double[] DoubArray = {64.542, 75.983}; 		
		double[] DoubleArray = Arrays.copyOf(DoubArray, 5);
		System.out.println("New Integer Array after Copying:");
		arrayPrint(DoubleArray);
		
		double[] dblArray = Arrays.copyOf(DoubArray, 4);
		dblArray[2] = 465.6054;
		dblArray[3] = 986.70534;
		System.out.println("New Integer Array with Vlaues:");
		arrayPrint(dblArray);
	}
	public static void arrayPrint(double[] anDoubleArray) {
		for (double Number: anDoubleArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Java Arrays.copyOf Method 6

Java Arrays.copyOf to copy Float Array

In this Java program, we declared the floating-point array with random array elements. Then we call the public static float copyOf (float[] anFloatArray, int newLength) to copy the float array to a new array of a specified length.

// Java Example to Copy Float Array to New Array
package ArrayMethods;
import java.util.Arrays;

public class CopyOfFloat {
	public static void main(String[] args) {
		float[] floatArray = {4.54f, 7.98f, 5.68f, 3.25f, 6.95f}; 		
		float[] fltArray = Arrays.copyOf(floatArray, 2);
		System.out.println("New Float Array after Copying:");
		arrayPrint(fltArray);
		
		float[] ftArray = Arrays.copyOf(floatArray, 4);
		ftArray[0] = 12.56f;
		ftArray[3] = 22.56f;
		System.out.println("New Float Array with Changed Vlaues:");
		arrayPrint(ftArray);
	}
	public static void arrayPrint(float[] anfloatArray) {
		for (float Number: anfloatArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Java Arrays.copyOf Method 7

Arrays.copyOf to copy Char Array

In this Java array copyof program, we declared the character array with random array elements. Next, we call the public static Char copyof (char[] anCharacterArray, int newLength) to copy Java Char array to a new character array of a specified length.

// Java Example to Copy Char Array to New Array
package ArrayMethods;
import java.util.Arrays;

public class CopyOfChar {
	public static void main(String[] args) {
		char[] CharArray = {'J', 'a', 'v', 'a'}; 
		char[] ChArray = Arrays.copyOf(CharArray, 5);
		System.out.println("New Character Array after Copying:");
		arrayPrint(ChArray);
		
		char[] ChrArray = Arrays.copyOf(CharArray, 6);
		ChrArray[4] = 'T';
		ChrArray[5] = 'G';
		System.out.println("New Character Array with Vlaues:");
		arrayPrint(ChrArray);
	}
	public static void arrayPrint(char[] anCharacterArray) {
		for (char Number: anCharacterArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Java Arrays.copyOf Method 8

Arrays.copyOf to copy Object Array

In this Java array copyof program, we declared the String array (string is an Object) with random array elements. Next, we call the public static T[] copyOf (T[] anArray, int newLength) to copy Java Object array to a new array of a specified length.

// Java Example to Copy Object Array to New Array
package ArrayMethods;
import java.util.Arrays;

public class CopyOfObject {
	public static void main(String[] args) {
		String[] stringArray = {"India", "USA"}; 
		String[] strArray = Arrays.copyOf(stringArray, 5);
		System.out.println("New Object Array after Copying:");
		arrayPrint(strArray);
		
		String[] strngArray = Arrays.copyOf(stringArray, 4);
		strngArray[2] = "Australia";
		strngArray[3] = "Japan";
		System.out.println("New Object Array with Vlaues:");
		arrayPrint(strngArray);
	}
	public static void arrayPrint(Object[] anObjectArray) {
		for (Object Number: anObjectArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Java Arrays.copyOf Method 9

Arrays.copyOf to copy Array to User specified Array type

In this Java program, we declared the Object array with random array elements. Then we will call the public static <T, U> T[] copyOf (U[] anObjectArray, int newLength, , Class<? extends T[]> newType) to copy Java Object array to new specified array.

// Java Example to Copy Object Array to New Array
package ArrayMethods;
import java.util.Arrays;

public class CopyOfT {
	public static void main(String[] args) {
		Object[] objectArray = {"Australia", "India"}; 
		String[] objArray = Arrays.copyOf(objectArray, 5, String[].class);
		System.out.println("New Object Array after Copying:");
		arrayPrint(objArray);
	}
	public static void arrayPrint(Object[] anObjectArray) {
		for (Object Number: anObjectArray) {
			System.out.println("Array Elelment = " + Number);
		}
	}
}
Java Arrays.copyOf Method 10

Filed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial

Copyright © 2021· All Rights Reserved by Suresh.
About | Contact | Privacy Policy