I wanted to create a list of options for testing purposes. At first, I did this:
ArrayList places = new ArrayList();
places.add(\
In Java, you can't do
ArrayList places = new ArrayList( Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
As was pointed out, you'd need to do a double brace initialization:
List places = new ArrayList() {{ add("x"); add("y"); }};
But this may force you into adding an annotation @SuppressWarnings("serial")
or generate a serial UUID which is annoying. Also most code formatters will unwrap that into multiple statements/lines.
Alternatively you can do
List places = Arrays.asList(new String[] {"x", "y" });
but then you may want to do a @SuppressWarnings("unchecked")
.
Also according to javadoc you should be able to do this:
List stooges = Arrays.asList("Larry", "Moe", "Curly");
But I'm not able to get it to compile with JDK 1.6.