Initialization of an ArrayList in one line.

Posted by Macarse on Stack Overflow See other posts from Stack Overflow or by Macarse
Published on 2009-06-17T04:10:20Z Indexed on 2010/05/04 0:48 UTC
Read the original article Hit count: 322

I am willing to create a list of options to test something.

I was doing:

ArrayList<String> places = new ArrayList<String>();
places.add("Buenos Aires");
places.add("Córdoba");
places.add("La Plata");

I refactor the code doing:

ArrayList<String> places = new ArrayList<String>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));

Is there a better way of doing this?

Thanks for reading!

© Stack Overflow or respective owner

Related posts about java

Related posts about arraylist