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

2
  • 1
    Could you add a language tag? Commented Dec 18, 2013 at 23:00
  • possible duplicate of this question Commented Dec 18, 2013 at 23:05

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I keep getting errors that ArrayList cannot be resolved to a type
@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.
Ahhh I forgot the import java.util.Arrays package.
0

Answered here

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

*** new

2 Comments

Instead of adding an answer pointing to an existing Q/A, flag the question as duplicate.
will do.. kind of new to SO

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.