arraylist二维数组
时间: 2023-12-09 19:32:29 浏览: 60
ArrayList 可以存储对象,因此可以使用 ArrayList 来存储二维数组。具体实现方法是将每一行作为一个 ArrayList,然后将这些 ArrayList 存储在一个外层的 ArrayList 中。
示例代码如下:
```
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
int[][] arr = {{1, 2}, {3, 4}, {5, 6}};
for (int[] row : arr) {
ArrayList<Integer> rowList = new ArrayList<>();
for (int num : row) {
rowList.add(num);
}
list.add(rowList);
}
```
这样就可以将二维数组 arr 转换成 ArrayList<ArrayList<Integer>> 类型的 list。
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)