pleerock pleerock - 3 years ago 142
Java Question

Java quick array list

I have a very simple question:

How can I make this code more simple on Java:

ArrayList<String> s = new ArrayList<String>();
s.add("str1");
s.add("str hello");
s.add("str bye");
//...


Something like that:

ArrayList<String> s = {"a1", "str", "mystr"};


or that:

ArrayList<String> s = new ArrayList<String>("a1", "str", "mystr");


or that:

ArrayList<String> s = new ArrayList<String>();
s.addAll("a1", "str", "mystr");


or that:

ArrayList<String> s = new ArrayList<String>();
s.addAll(new ArrayElements("a1", "str", "mystr"));


I just want syntax hint. Thanks.

NPE NPE
Answer Source

How about:

ArrayList<String> s = new ArrayList<String>();
Collections.addAll(s, "a1", "str", "mystr");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download