It would be simpler if you were to just declare it as a fixed-size List (but with mutable elements) - does it have to be an ArrayList?
List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");
Or if you have only one element:
List<String> places2 = Collections.singletonList("Buenos Aires");
This would mean that places2 is immutable (trying to change it in any way will cause an UnsupportedOperationException exception to be thrown).
To make a variable-size list that is a concrete ArrayList you can create an ArrayList from the fixed-size list:
ArrayList<String> places = new ArrayList<>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
And import the correct package:
import java.util.Arrays;
Answer from Tom on Stack OverflowIt would be simpler if you were to just declare it as a fixed-size List (but with mutable elements) - does it have to be an ArrayList?
List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");
Or if you have only one element:
List<String> places2 = Collections.singletonList("Buenos Aires");
This would mean that places2 is immutable (trying to change it in any way will cause an UnsupportedOperationException exception to be thrown).
To make a variable-size list that is a concrete ArrayList you can create an ArrayList from the fixed-size list:
ArrayList<String> places = new ArrayList<>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
And import the correct package:
import java.util.Arrays;
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.add("C");
The catch is that there is quite a bit of typing required to refer to that list instance.
There are alternatives, such as making an anonymous inner class with an instance initializer (also known as an "double brace initialization"):
ArrayList<String> list = new ArrayList<String>() {{
add("A");
add("B");
add("C");
}};
However, I'm not too fond of that method because what you end up with is a subclass of ArrayList which has an instance initializer, and that class is created just to create one object -- that just seems like a little bit overkill to me.
What would have been nice was if the Collection Literals proposal for Project Coin was accepted (it was slated to be introduced in Java 7, but it's not likely to be part of Java 8 either.):
List<String> list = ["A", "B", "C"];
Unfortunately it won't help you here, as it will initialize an immutable List rather than an ArrayList, and furthermore, it's not available yet, if it ever will be.
java - ArrayList initialization equivalent to array initialization - Stack Overflow
Initializing an empty ArrayList (Java)
Best Practice for Initializing ArrayLists with values?
How do I declare an ArrayList in a default constructor?
Videos
Arrays.asList can help here:
new ArrayList<Integer>(Arrays.asList(1,2,3,5,8,13,21));
Yes.
new ArrayList<String>(){{
add("A");
add("B");
}}
What this is actually doing is creating a class derived from ArrayList<String> (the outer set of braces do this) and then declare a static initialiser (the inner set of braces). This is actually an inner class of the containing class, and so it'll have an implicit this pointer. Not a problem unless you want to serialise it, or you're expecting the outer class to be garbage collected.
I understand that Java 7 will provide additional language constructs to do precisely what you want.
EDIT: recent Java versions provide more usable functions for creating such collections, and are worth investigating over the above (provided at a time prior to these versions)