The Java Arrays.copyOf Method is one of the Java Array Method which is used to copy the array content into new array of user specified length.
In this article we will show you, How to copy Java Array to new array with example. Before we get into the example, 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 user specified length is greater than the Original Array, then 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 user specified length is less than the Original Array, then 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 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);
Following Java arrays.copyof method will accept the Byte Array and Array length as the parameters and copies the Java Byte array to 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 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);
Following method will accept the Character Array and Array length as the parameters and copies the Java char array to 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);
Following method will accept the Integer Array and Array length as the parameters and copies the Java Integer array to 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 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);
Following method will accept the Double Array and Array length as the parameters and copies the Java Double array to 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);
Following method will accept the Float Array and Array length as the parameters and copies the Java Float array to new Float array of user specified 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 accept 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);
Following method will accept the Object Array and Array length as the parameters and copies the Java Object array to 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 new array.
- newlength: Please specify the length of new array. Based on the length, array elements will be 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, and then we will call the public static byte copyOf (byte[] anByteArray, int newLength) method to copy the Java Byte array to new array of specified length.
JAVA CODE
// 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); } } }
OUTPUT
ANALYSIS
In this This Java array copyof method example, First, we declared a byte Array and assigned some random values as the array elements using 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. This will copy the available elements from byteArray to bitArray and fill the remaining values with default value (which is zero in this case)
byte[] bitArray = Arrays.copyOf(byteArray, 5);
Following statement is used 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 default values (zeros) will be replaced by these values
byte[] btArray = Arrays.copyOf(byteArray, 5); btArray[3] = 5; btArray[4] = 19;
Following statement is used to print the Byte array elements to the output
arrayPrint(byteArray);
When the compiler reaches to the above statement, compiler will jump to following function. From the below code snippet you can observe that, we used the Java Foreach Loop to iterate the Byte Array and then we are printing each and 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); } }
Java Arrays.copyOf to copy Boolean Array
In this Java program we declared the Boolean array with random array elements, and then we will call the public static Boolean copyOf (boolean[] anBooleanArray, int newLength) method to copy the Java Boolean array to new array of specified length.
JAVA CODE
// 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); } } }
OUTPUT
Java Arrays.copyOf to copy Short Array
In this Java program we declared the short array with random array elements, and then we will call the public static Short copyOf (short[] anShortArray, int newLength) method to copy the Java short array to new array of specified length.
JAVA CODE
// 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); } } }
OUTPUT
Java Arrays.copyOf to copy Integer Array
In this Java program we declared the integer array with random array elements, and then we will call the public static Int copyOf (int[] anIntegerArray, int newLength) method to copy the Java Int array to new array of specified length.
JAVA CODE
// 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); } } }
OUTPUT
Java Arrays.copyOf to copy Long Array
In this Java program we declared the long array with random array elements, and then we will call the public static Long copyOf (long[] anLongArray, int newLength) method to copy the Java Long array to new array of specified length.
JAVA CODE
// 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); } } }
OUTPUT
Java 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) method to copy the Java double array to new array of specified length.
JAVA CODE
// 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); } } }
OUTPUT
Java Arrays.copyOf to copy Float Array
In this Java program we declared the floating-point array with random array elements, and then we will call the public static float copyOf (float[] anFloatArray, int newLength) method to copy the Java float array to new array of specified length.
JAVA CODE
// 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); } } }
OUTPUT
Java 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) method to copy Java Char array to new character array of specified length.
JAVA CODE
// 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); } } }
OUTPUT
Java 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 will call the public static T[] copyOf (T[] anArray, int newLength) method to copy Java Object array to new array of specified length.
JAVA CODE
// 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); } } }
OUTPUT
Java Arrays.copyOf to copy Array to User specified Array type
In this Java program we declared the Object array with random array elements, and then we will call the public static <T, U> T[] copyOf (U[] anObjectArray, int newLength, , Class<? extends T[]> newType) method to copy Java Object array to new specified array.
JAVA CODE
// 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); } } }
OUTPUT
Thank You for Visiting Our Blog