请注意以下代码:
private List<List<Place>> m_grid = constructGrid(10000);
private static List<List<Place>> constructGrid(int size) {
List<List<Place>> res = new ArrayList<List<Place>>(size);
for (int i = 0; i < size; ++i) {
res.add(null);
}
return res;
}
很沉闷。有没有更漂亮的方法来做同样的事情?使用某种标准库的单线程?
感谢。
修改
列表必须是可变的。因此,Collections.nCopies
不适合该法案。
答案 0 :(得分:4)
我真的不明白为什么你的列表中需要10000
null,但如果你想这样做,你可以这样做:
List<List<Place>> tmp = Collections.nCopies(10000, null); // immutable
List<List<Place>> res = new ArrayList<List<Place>>(tmp); // mutable