11

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

Dingles
  • 149
  • 2
  • 3
  • 8

2 Answers2

1

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.

Tom Leese
  • 19,309
  • 12
  • 45
  • 70
  • I keep getting errors that `ArrayList` cannot be resolved to a type – Dingles Dec 18 '13 at 23:25
  • @Dingles make sure you have imported the right classes from the right packages. You should have `package your.package.name; import java.util.ArrayList; import java.util.Arrays; (...)` at the top of your file. – Luiggi Mendoza Dec 19 '13 at 01:02
  • Ahhh I forgot the `import java.util.Arrays` package. – Dingles Dec 19 '13 at 03:06
0

Answered here

new ArrayList<Element>(Arrays.asList(array))

*** new

Community
  • 1
  • 1
JayD
  • 6,173
  • 4
  • 20
  • 24