I have trouble understanding why the conversion to primitive array fails for a one dimensional array but works for the two dimensional case.
public static void main( String[] args ) throws IOException {
List<Integer> oneD = List.of(1,2,3);
int[] one = oneD.toArray(int[]::new); // error
List<int[]> twoD = List.of(
new int[]{1,2},
new int[]{3,4}
);
int[][] two = twoD.toArray(int[][]::new); // works
}
I tried making my way through the documentation but of no avail. One thing I can see is that in the first case, since we are trying to convert from Integer to int, is what might be causing the error, and that the second case works fine since int[] would be an Object type.
An explanation with an example of how toArray works, especially for primitives would be really helpful.