- Java.util Package Classes
- 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 Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Arrays copyOfRange() Method
Description
The Java Arrays copyOfRange(double[] original, int from, int to) method copies the specified range of the specified array into a new array.The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case 0d is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the returned array will be to - from.
Declaration
Following is the declaration for java.util.Arrays.copyOfRange() method
public static double[] copyOfRange(double[] original, int from, int to)
Parameters
original − This is the array from which a range is to to be copied.
from − This is the initial index of the range to be copied, inclusive.
to − This is the final index of the range to be copied, exclusive.
Return Value
This method returns a new array containing the specified range from the original array, truncated or padded with zeros to obtain the required length.
Exception
ArrayIndexOutOfBoundsException − If from < 0 or from > original.length()
IllegalArgumentException − If if from > to.
NullPointerException − If original is null.
Copying an Array of doubles to a Same Sized Array Example
The following example shows the usage of Java Arrays copyOfRange() method. First, we've created an array of doubles. We've printed them. A copy of array with same size is created using copyOfRange() method and printed.
package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { double[] doubleArr = { 10.0, 20.0, 30.0, 13.0 }; System.out.print("Double Array: ["); for (int i = 0; i < doubleArr.length; i++) { System.out.print(doubleArr[i] + " "); } System.out.print("]\nCopied Array: \n"); // Create copy of the array of same size double[] doubleArrCopy = Arrays.copyOfRange(doubleArr, 0, 4); System.out.print("Double Array: ["); for (int i = 0; i < doubleArrCopy.length; i++) { System.out.print(doubleArrCopy[i] + " "); } System.out.print("]"); } }
Output
Let us compile and run the above program, this will produce the following result −
Double Array: [10.0, 20.0, 30.0, 13.0 ] Copied Array: Double Array: [10.0, 20.0, 30.0, 13.0 ]
Copying an Array of doubles to a Higher Sized Array Example
The following example shows the usage of Java Arrays copyOfRange() method. First, we've created an array of doubles. We've printed them. A copy of array with higher size is created using copyOfRange() method and printed with default values appended.
package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { double[] doubleArr = { 10.0, 20.0, 30.0, 13.0 }; System.out.print("Double Array: ["); for (int i = 0; i < doubleArr.length; i++) { System.out.print(doubleArr[i] + " "); } System.out.print("]\nCopied Arrays: \n"); // Create copy of the array of greater size double[] doubleArrCopy = Arrays.copyOfRange(doubleArr, 1, 6); System.out.print("Double Array: ["); for (int i = 0; i < doubleArrCopy.length; i++) { System.out.print(doubleArrCopy[i] + " "); } System.out.print("]"); } }
Output
Let us compile and run the above program, this will produce the following result −
Double Array: [10.0 20.0 30.0 13.0 ] Copied Arrays: Double Array: [20.0 30.0 13.0 0.0 0.0 ]
Copying an Array of doubles to a Lower Sized Array Example
The following example shows the usage of Java Arrays copyOfRange() method. First, we've created an array of doubles. We've printed them. A copy of array with lower size is created using copyOfRange() method and printed.
package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { double[] doubleArr = { 10.0, 20.0, 30.0, 13.0 }; System.out.print("Double Array: ["); for (int i = 0; i < doubleArr.length; i++) { System.out.print(doubleArr[i] + " "); } System.out.print("]\nCopied Arrays: \n"); // Create copy of the array of lesser size double[] doubleArrCopy = Arrays.copyOfRange(doubleArr, 1, 3); System.out.print("Double Array: ["); for (int i = 0; i < doubleArrCopy.length; i++) { System.out.print(doubleArrCopy[i] + " "); } System.out.print("]"); } }
Output
Let us compile and run the above program, this will produce the following result −
Double Array: [10.0 20.0 30.0 13.0 ] Copied Arrays: Double Array: [20.0 30.0 ]