Program
import java.util.Arrays; public class ArrayCopyOfRange { public static void main(String[] args) { int[] arr = new int[] { 15, 10, 45 }; System.out.println("Printing Default array:"); for(int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } Object copyOfRange = Arrays.copyOfRange(arr, 1, 3); int[] arr2 = (int[]) copyOfRange; System.out.println("Printing new array:"); for(int i = 0; i < arr2.length; i++) { System.out.println(arr2[i]); } } }
Output
Printing Default array: 15 10 45 Printing new array: 10 45
Description
public static int[] copyOfRange​(int[] original, int from, int to)
Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to). Values from subsequent elements in the original array are placed into subsequent elements in the copy. 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 0 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.
Parameters:
original – the array from which a range is to be copied
from – the initial index of the range to be copied, inclusive
to – the final index of the range to be copied, exclusive. (This index may lie outside the array.)
Returns:
a new array containing the specified range from the original array, truncated or padded with zeros to obtain the required length
Throws:
ArrayIndexOutOfBoundsException – if from < 0 or from > original.length
IllegalArgumentException – if from > to
NullPointerException – if original is null
Since:
1.6