How to add many values to an arraylist at once?
假设我有以下代码:
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);
可以:
list.addAll(Arrays.asList(a, b, c, d, e));
也可以:
ArrayList<String> list = Lists.newArrayList(a, b, c, d, e);