I have a List of arrays of integers. I want to convert it to an array of arrays of integers, but when using toArray() I get the error [Ljava.lang.Object; cannot be cast to [[I
What am I doing wrong? Thanks
public int[][] getCells() {
List<int[]> cells = new ArrayList<>();
for (int y = this.y0; y <= this.y1 ; y++) {
for (int x = this.x0; x <= this.x1 ; x++) {
cells.add(new int[]{x, y});
}
}
return (int[][]) cells.toArray();
}
EDIT
I've seen that in this case I can convert it with:
return cells.toArray(new int[cells.size()][2]);
But only because all the integer arrays have the same size (2). What If I don't know it? Thanks