EN
Java - create subarray of ints from another array
11 points
In java there is couple of ways how to create subarray, the simples way I know with simple code example is presented here.
Syntax:
xxxxxxxxxx
1
int[] copyOfRange(int[] original, int from, int to)
Code example:
xxxxxxxxxx
1
int[] arr = {10, 20, 30, 40, 50, 60};
2
int[] subArr1 = Arrays.copyOfRange(arr, 0, 2);
3
int[] subArr2 = Arrays.copyOfRange(arr, 2, 6);
4
5
System.out.println(Arrays.toString(arr)); // [10, 20, 30, 40, 50, 60]
6
System.out.println(Arrays.toString(subArr1)); // [10, 20]
7
System.out.println(Arrays.toString(subArr2)); // [30, 40, 50, 60]
Output:
xxxxxxxxxx
1
[10, 20, 30, 40, 50, 60]
2
[10, 20]
3
[30, 40, 50, 60]