ArrayList initialization equivalent to array initialization

Jonathan picture Jonathan · May 3, 2010 · Viewed 209.1k times · Source

I am aware that you can initialize an array during instantiation as follows:

String[] names = new String[] {"Ryan", "Julie", "Bob"};

Is there a way to do the same thing with an ArrayList? Or must I add the contents individually with array.add()?

Answer

meriton picture meriton · May 3, 2010

Arrays.asList can help here:

new ArrayList<Integer>(Arrays.asList(1,2,3,5,8,13,21));