Reputation: 1532
I have a really simple question, but I just want to know what the correct way of doing it is.
How do you convert a variable of type ArrayList<int[]>
to an int[][]
?
Upvotes: 1
Views: 531
Reputation: 1108632
Use List#toArray(T[])
wherein you pass the array type.
int[][] intMatrix = listOfIntArrays.toArray(new int[0][]);
Demo:
List<int[]> listOfIntArrays = new ArrayList<int[]>();
listOfIntArrays.add(new int[] { 10, 20, 30 });
listOfIntArrays.add(new int[] { 11, 21, 31 });
listOfIntArrays.add(new int[] { 12, 22, 32 });
int[][] intMatrix = listOfIntArrays.toArray(new int[0][]);
System.out.println(Arrays.deepToString(intMatrix));
Result:
[[10, 20, 30], [11, 21, 31], [12, 22, 32]]
Upvotes: 10
Reputation: 2446
let a = ArrayList<int[]>
then to convert it to an int[][]
you say, for example, int[][] b = a.toArray(new int[a.size()][])
Upvotes: 1