forum

Home / DeveloperSection / Forums / Initialization of an ArrayList in one line

Initialization of an ArrayList in one line

Royce Roy 2079 12-May-2015
I want to create a list of options for testing purposes. At first, I did this:

ArrayList<String> places = new ArrayList<String>();
places.add("Buenos Aires");
places.add("Córdoba");
places.add("La Plata");
Then I refactored the code as follows:

ArrayList<String> places = new ArrayList<String>(
    Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
Is there a better way to do this?

Updated on 12-May-2015

Can you answer this question?


Answer

1 Answers

Liked By