1import java.util.Arrays;
2public class SlicingAnArray {
3 public static void main(String args[]){
4 int intArray[] = {12, 14, 58, 225, 56, 96 , 3, 45, 8 };
5 intArray = Arrays.copyOfRange(intArray, 3, 7);
6 System.out.println(Arrays.toString(intArray));
7 }
8}
1 //index 0 1 2 3 4
2int[] arr = {10, 20, 30, 40, 50};
3Arrays.copyOfRange(arr, 0, 2); // returns {10, 20}
4Arrays.copyOfRange(arr, 1, 4); // returns {20, 30, 40}
5Arrays.copyOfRange(arr, 2, arr.length); // returns {30, 40, 50} (length = 5)
6
1import java.util.IntStream;
2// for java 8 and 8+
3IntStream.range(start, end).map(x -> arr[x]).toArray();