Like Tom said:
List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");
But since you complained of wanting an ArrayList, you should first know that ArrayList is a subclass of List and you could simply add this line:
ArrayList<String> myPlaces = new ArrayList(places);
Although, that might make you complain about 'performance'.
In that case, it doesn't make sense to me, why, since your list is predefined it wasn't defined as an array (since the size is known at the time of initialization). And if that's an option for you:
String[] places = { "Buenos Aires", "Córdoba", "La Plata" };
In case you don't care about the minor performance differences then you can also copy an array to an ArrayList very simply:
ArrayList<String> myPlaces = new ArrayList(Arrays.asList(places));
Okay, but in the future, you need a bit more than just the place name, you need a country code too. Assuming this is still a predefined list that will never change during run-time, then it's fitting to use an enum
set, which would require re-compilation if the list needed to be changed in the future.
enum Places { BUENOS_AIRES, CORDOBA, LA_PLATA }
would become:
enum Places { BUENOS_AIRES("Buenos Aires", 123), CORDOBA("Córdoba", 456), LA_PLATA("La Plata", 789); String name; int code; Places(String name, int code) { this.name = name; this.code = code; }}
Enums have a static values
method that returns an array containing all of the values of the enum in the order they are declared, e.g.:
for (Places p : Places.values()) { System.out.printf("The place %s has code %d%n", p.name, p.code);}
In that case, I guess you wouldn't need your ArrayList.
P.S. Randyaa demonstrated another nice way using the static utility method Collections.addAll.