【发布时间】:2016-05-06 17:55:20
【问题描述】:
我想创建一个具有 n 行和 m 列的 ArrayList-matrix,例如
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
我已经编写了用于创建这样一个矩阵的代码,但是当我清除包含列数据的列表时,我的代码不显示值。这是我的
package testproject;
import java.util.ArrayList;
public class TestProject {
public static void main(String[] args) {
ArrayList<Integer> intList = new ArrayList<>();
ArrayList<ArrayList<Integer>> mainList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
for (int k = 0; k < 5; k++) {
intList.add(k);
}
mainList.add(intList);
intList.clear(); //this line is probably the problem
}
for (int row = 0; row < mainList.size(); row++) {
for (int col = 0; col < mainList.get(0).size(); col++) {
System.out.print(mainList.get(row).get(col) + ",");
}
System.out.println("");
}
}
}
有没有可能在不清除intList的内容的情况下清除 mainList 的内容??
【问题讨论】:
-
你不能只声明一个新的 ArrayList
newList = new ArrayList (intList);然后将其添加到 mainList 而不是 intList?