在 python 中,我可以执行以下操作:
n = 8
a = []
a += [1]*n
我如何在 java 中用 ArrayLists 做同样的事情(不使用 for 循环..)?
List<T> list = new ArrayList<T>();
list.add(1);
list.add(2);
list.add(3);
// Some construct that is equivalent to a += [1]*n
最佳答案
final List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.addAll(Collections.nCopies(8, 1));
关于Java 用 n 个值的列表扩展 arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20138275/