Let’s learn Arrays copyOfRange() method in java.
Arrays copyOfRange() method in java
Arrays copyOfRange() method in java copies specified range of the original array into a new array. The initial index of the range (from) must lie between 0 and original.length, inclusive.
Syntax:
public static int[] copyOfRange(int[] original_array, int from_index, int to_index)
original_array: array from which a range is to be copied from.
from_index: initial or starting index of range to be copied, inclusive.
to_index: ending or final index of range to be copied, exclusive.(This index may lie outside the array.)
Exceptions thrown:
ArrayIndexOutOfBoundsException – if from < 0 or from > original.length.
IllegalArgumentException – if from > to.
NullPointerException – if original is null.
Note:
- Initial index (from) should lie between 0 and original.length (inclusive).
- Value at original is placed to the initial element of copy. Except for from == original.length or from == to.
- Final index (to) should be greater than or equal to from. In some cases it could be greater than original.length in this case 0 is placed in all elements of copy whose index is >= original.length (from).
- Length of returned array will be to – from.
Let’s see an example for Arrays copyOfRange() method.
import java.util.Arrays; public class ArrayCopyOfRangeDemo { public static void main(String[] args) { int[] arrNumber = { 66, 67, 68, 69, 70, 71, 72 }; System.out.println("Given array: "); for(int a = 0; a < arrNumber.length; a++) { System.out.println(arrNumber[a]); } int[] copyNum = Arrays.copyOfRange(arrNumber, 2, 6); System.out.println("Between range 2 and 6: "); for(int a : copyNum) { System.out.print(a + " "); } System.out.println(); } }
Output:
Given array:
66
67
68
69
70
71
72
Between range 2 and 6:
68 69 70 71
Overloaded forms of ArrayscopyOfRange() method
Method | Description |
static long[] copyOfRange(long[] original, int from, int to) | Copies the specified range of the specified array into a new array. |
static short[] copyOfRange( short[] original, int from, int to) | Copies the specified range of the specified array into a new array. |
static T[] copyOfRange(T[] original, int from, int to) | Copies the specified range of the specified array into a new array. |
static T[] copyOfRange(U[] original, int from, int to, Class newType) | Copies the specified range of the specified array into a new array. |
static int[] copyOfRange(int[] original, int from, int to) | Copies the specified range of the specified array into a new array. |
static float[] copyOfRange(float[] original, int from, int to) | Copies the specified range of the specified array into a new array. |
static double[] copyOfRange( double[] original, int from, int to) | Copies the specified range of the specified array into a new array. |
static char[] copyOfRange(char[] original, int from, int to) | Copies the specified range of the specified array into a new array. |
static byte[] copyOfRange(byte[] original, int from, int to) | Copies the specified range of the specified array into a new array. |
static boolean[] copyOfRange( boolean[] original, int from, int to) | Copies the specified range of the specified array into a new array. |
Also read – java overview