解决二维数组转为ArrayList集合问题

1.修改前代码块

1 String[][] str = { { "语文", "100" }, { "英语", "90" }, { "数学", "90" }, };
2     ArrayList<String> list = new ArrayList<String>();
3     for (int i = 0; i < str.length; i++) {
4         for (int j = 0; j < str[i].length; j++) {
5             list.add(str[i][j]);
6         }
7     }
8     System.out.println(list);

控制台输出结果: ”[语文, 100,英语, 90,数学, 90]”

2.修改后代码块

 1 String[][] str = { { "语文", "100" }, { "英语", "90" }, { "数学", "90" } };
 2         ArrayList<String> list = null;
 3         ArrayList<ArrayList<String>> list2 = new ArrayList<ArrayList<String>>();
 4         for (int i = 0; i < str.length; i++) {
 5             list = new ArrayList<String>();
 6             for (int j = 0; j < str[i].length; j++) {
 7                 list.add(str[i][j]);
 8             }
 9             list2.add(list);
10         }
11         Iterator it = list2.iterator();
12         while (it.hasNext()) {
13             System.out.println(it.next());
14         }

控制台输出的结果为:
[语文, 100]
[英语, 90]
[数学, 90]

原文地址:https://www.cnblogs.com/sunny-sl/p/7815162.html