📜  Java 7. 转换列表<List<Integer> &gt; to int[][]: - 任何代码示例

📅  最后修改于: 2022-03-11 14:58:16.508000             🧑  作者: Mango

代码示例1
// original list
List> list = Arrays.asList(
        Arrays.asList(1, 2),
        Arrays.asList(2),
        Arrays.asList(3));
// initialize the array,
// specify only the number of rows
int[][] arr = new int[list.size()][];
// iterate through the array rows
for (int i = 0; i < arr.length; i++) {
    // initialize the row of the array,
    // specify the number of elements
    arr[i] = new int[list.get(i).size()];
    // iterate through the elements of the row
    for (int j = 0; j < arr[i].length; j++) {
        // populate the array
        arr[i][j] = list.get(i).get(j);
    }
}
// output
System.out.println(Arrays.deepToString(arr));
// [[1, 2], [2], [3]]