Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing an Array to an ArrayList [duplicate]

I am attempting to change the following array line into an ArrayList that will function the same way:

private String[] books = new String[5];

I changed it to this but it is not functioning properly:

private ArrayList<String>(Arrays.asList(books))

I thought this was how an ArrayList was created

like image 833
Dingles Avatar asked Dec 18 '13 22:12

Dingles


1 Answers

You need to create it like this:

private ArrayList<String> booksList = new ArrayList<String>(Arrays.asList(books));

new ArrayList<String>(Arrays.asList(books)) is the part which is turning your array into an ArrayList.

You could also do:

private List<String> booksList = Arrays.asList(books);

If the fact that it is an ArrayList doesn't matter.

like image 199
Tom Leese Avatar answered Nov 15 '22 00:11

Tom Leese