
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Arrays copyOf() Method
Description
The Java Arrays copyOf(U[] original, int newLength, Class<? extends T[]> newType) method copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain false. Such indices will exist if and only if the specified length is greater than that of the original array.
Declaration
Following is the declaration for java.util.Arrays.copyOf(T[] original,int newLength) method
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType)
Type Parameters
T − This is the class of the objects in the returned array.
U − This is the class of the objects in the original array.
Parameters
original − This is the array to be copied.
newLength − This is the length of the copy to be returned.
newType − This is the class of the copy to be returned.
Return Value
This method returns a copy of the original array, truncated or padded with false elements to obtain the specified length.
Exception
NegativeArraySizeException − If newLength is negative.
NullPointerException − If original is null.
ArrayStoreException − if an element copied from original is not of a runtime type that can be stored in an array of class newType.
Copying an Array of Objects to a Same Sized Array Example
The following example shows the usage of Java Arrays copyOf() method. First, we've created an array of Monitor objects. We've printed them. A copy of array with same size is created using copyOf() method and printed.
package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { Student[] studentArr = { new Monitor(1, "Julie", false), new Monitor(2, "Robert", true), new Monitor(3, "Adam", false) }; System.out.print("Student Array: ["); for (int i = 0; i < studentArr.length; i++) { System.out.print(studentArr[i] + " "); } System.out.print("]\nCopied Arrays: \n"); // Create copy of the array of same size Monitor[] studentArrCopy = Arrays.copyOf(studentArr, studentArr.length, Monitor[].class); System.out.print("Student Array: ["); for (int i = 0; i < studentArrCopy.length; i++) { System.out.print(studentArrCopy[i] + " "); } System.out.print("]"); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } } class Monitor extends Student{ boolean priviledges; Monitor(int rollNo, String name, boolean priviledges) { super(rollNo, name); this.priviledges = priviledges; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + ", " + this.priviledges + " ]"; } }
Output
Let us compile and run the above program, this will produce the following result −
Student Array: [[ 1, Julie, false ] [ 2, Robert, true ] [ 3, Adam, false ] ] Copied Arrays: Student Array: [[ 1, Julie, false ] [ 2, Robert, true ] [ 3, Adam, false ] ]
Copying an Array of Objects to a Higher Sized Array Example
The following example shows the usage of Java Arrays copyOf() method. First, we've created an array of Monitor objects. We've printed them. A copy of array with higher size is created using copyOf() method and printed with default values appended.
package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { Student[] studentArr = { new Monitor(1, "Julie", false), new Monitor(2, "Robert", true), new Monitor(3, "Adam", false) }; System.out.print("Student Array: ["); for (int i = 0; i < studentArr.length; i++) { System.out.print(studentArr[i] + " "); } System.out.print("]\nCopied Arrays: \n"); // Create copy of the array of greater size Monitor[] studentArrCopy = Arrays.copyOf(studentArr, studentArr.length + 1, Monitor[].class); System.out.print("Student Array: ["); for (int i = 0; i < studentArrCopy.length; i++) { System.out.print(studentArrCopy[i] + " "); } System.out.print("]"); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } } class Monitor extends Student{ boolean priviledges; Monitor(int rollNo, String name, boolean priviledges) { super(rollNo, name); this.priviledges = priviledges; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + ", " + this.priviledges + " ]"; } }
Output
Let us compile and run the above program, this will produce the following result −
Student Array: [[ 1, Julie, false ] [ 2, Robert, true ] [ 3, Adam, false ] ] Copied Arrays: Student Array: [[ 1, Julie, false ] [ 2, Robert, true ] [ 3, Adam, false ] null ]
Copying an Array of Objects to a Lower Sized Array Example
The following example shows the usage of Java Arrays copyOf() method. First, we've created an array of Monitor objects. We've printed them. A copy of array with lower size is created using copyOf() method and printed.
package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { Student[] studentArr = { new Monitor(1, "Julie", false), new Monitor(2, "Robert", true), new Monitor(3, "Adam", false) }; System.out.print("Student Array: ["); for (int i = 0; i < studentArr.length; i++) { System.out.print(studentArr[i] + " "); } System.out.print("]\nCopied Arrays: \n"); // Create copy of the array of lesser size Monitor[] studentArrCopy = Arrays.copyOf(studentArr, studentArr.length - 1, Monitor[].class); System.out.print("Student Array: ["); for (int i = 0; i < studentArrCopy.length; i++) { System.out.print(studentArrCopy[i] + " "); } System.out.print("]"); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } } class Monitor extends Student{ boolean priviledges; Monitor(int rollNo, String name, boolean priviledges) { super(rollNo, name); this.priviledges = priviledges; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + ", " + this.priviledges + " ]"; } }
Output
Let us compile and run the above program, this will produce the following result −
Student Array: [[ 1, Julie, false ] [ 2, Robert, true ] [ 3, Adam, false ] ] Copied Arrays: Student Array: [[ 1, Julie, false ] [ 2, Robert, true ] ]