Quantcast
Channel: Initialization of an ArrayList in one line - Stack Overflow
Viewing all articles
Browse latest Browse all 37

Initialization of an ArrayList in one line

$
0
0

I wanted 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?


Viewing all articles
Browse latest Browse all 37

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>