Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 608
pleerock Avatar asked Oct 14 '11 12:10

pleerock


People also ask

What is list of () in Java?

The Java. util. List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements.

How do you create and initialize an ArrayList in one line?

Actually, probably the "best" way to initialize the ArrayList is the method you wrote, as it does not need to create a new List in any way: ArrayList<String> list = new ArrayList<String>(); list. add("A"); list. add("B"); list.


1 Answers

List<String> s = Arrays.asList("a1", "str", "mystr");

like image 180
dty Avatar answered Oct 14 '22 07:10

dty