There could be multiple ways to do it. If there is no issue of space, I would prefer
Arrays.copyOfRange()
In it, the
copyOfRange()
creates a new array of the same type as the original array, and contains the items of the specified range of the original array into a new array.
Syntax:
public static T[] copyOfRange(T[] original, int from, int to)
More details:
Java.util.Arrays.copyOfRange() Method[
^]
Example usage:
int[] arr1 = new int[] {15, 10, 45, 55};
int chunk = 2;
for(int i=0; i<arr1.length; i+=chunk){
System.out.println(Arrays.toString(Arrays.copyOfRange(arr1, i, Math.min(arr1.length,i+chunk))));
}