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.
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.
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.
List<String> s = Arrays.asList("a1", "str", "mystr");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With