关于java:如何一次向arraylist添加多个值?

How to add many values to an arraylist at once?

假设我有以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
String a =" some texte";
String b =" text";
String c ="sf";
String d =" kjel";
String e ="lkjl";

ArrayList<String> list = new ArrayList<String>();
// better way to do all these adds without having to type them all?
list.add(a);
list.add(b);
list.add(c);
list.add(d);
list.add(e);

如何使打字和计算更有效?


在一行中,您可以:

1
list.addAll(Arrays.asList(a, b, c, d, e));

你也可以使用番石榴:

1
 ArrayList<String> list = Lists.newArrayList(a, b, c, d, e);