
- 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(float[] original,int newLength) 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(float[] original,int newLength) method
public static float[] copyOf(float[] original,int newLength)
Parameters
original − This is the array to be copied.
newLength − This is the length 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.
Copying an Array of floats to a Same Sized Array Example
The following example shows the usage of Java Arrays copyOf() method. First, we've created an array of floats. 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) { float[] floatArr = { 10.0f, 20.0f, 30.0f, 13.0f }; System.out.print("Float Array: ["); for (int i = 0; i < floatArr.length; i++) { System.out.print(floatArr[i] + " "); } System.out.print("]\nCopied Array: \n"); // Create copy of the array of same size float[] floatArrCopy = Arrays.copyOf(floatArr, floatArr.length); System.out.print("Float Array: ["); for (int i = 0; i < floatArrCopy.length; i++) { System.out.print(floatArrCopy[i] + " "); } System.out.print("]"); } }
Output
Let us compile and run the above program, this will produce the following result −
Float Array: [10.0, 20.0, 30.0, 13.0 ] Copied Array: Float Array: [10.0, 20.0, 30.0, 13.0 ]
Copying an Array of floats to a Higher Sized Array Example
The following example shows the usage of Java Arrays copyOf() method. First, we've created an array of floats. 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) { float[] floatArr = { 10.0f, 20.0f, 30.0f, 13.0f }; System.out.print("Float Array: ["); for (int i = 0; i < floatArr.length; i++) { System.out.print(floatArr[i] + " "); } System.out.print("]\nCopied Arrays: \n"); // Create copy of the array of greater size float[] floatArrCopy = Arrays.copyOf(floatArr, floatArr.length + 1); System.out.print("Float Array: ["); for (int i = 0; i < floatArrCopy.length; i++) { System.out.print(floatArrCopy[i] + " "); } System.out.print("]"); } }
Output
Let us compile and run the above program, this will produce the following result −
Float Array: [10.0, 20.0, 30.0, 13.0 ] Copied Array: Float Array: [10.0, 20.0, 30.0, 13.0 0.0]
Copying an Array of floats to a Lower Sized Array Example
The following example shows the usage of Java Arrays copyOf() method. First, we've created an array of floats. 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) { float[] floatArr = { 10.0f, 20.0f, 30.0f, 13.0f }; System.out.print("Float Array: ["); for (int i = 0; i < floatArr.length; i++) { System.out.print(floatArr[i] + " "); } System.out.print("]\nCopied Arrays: \n"); // Create copy of the array of lesser size float[] floatArrCopy = Arrays.copyOf(floatArr, floatArr.length - 1); System.out.print("Float Array: ["); for (int i = 0; i < floatArrCopy.length; i++) { System.out.print(floatArrCopy[i] + " "); } System.out.print("]"); } }
Output
Let us compile and run the above program, this will produce the following result −
Float Array: [10.0, 20.0, 30.0, 13.0 ] Copied Array: Float Array: [10.0, 20.0, 30.0 ]