I want to split an array of integers into multiple subArrays of size n.How can this be achieved?
I tried using following way but it gives OutOfMemoryError
import java.util.*;
public class SplittingArraysIntoSubArrays {
public static void main(String[] args) {
int[] arrr = {1,3,2,5,4,6,8,13};
List<int[]> arrayList = new ArrayList<>();
int arrayLength = arrr.length;
int n =3;
int counter=1;
while(n*counter<arrayLength) {
arrayList.add(Arrays.copyOf(arrr, 3));
counter++;
}
System.out.println("Done");
}
}
Below is the output i get
[{1,3,2},{1,3,2}]
arrr.lengthis always going to be greater than 0 (zero), hence yourwhileloop never terminates.Gatherers#windowFixedGatherer available in Java 24 - javadoc