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
asked Dec 18, 2013 at 22:59
Dingles
1492 gold badges3 silver badges8 bronze badges
-
1Could you add a language tag?Floris– Floris2013年12月18日 23:00:29 +00:00Commented Dec 18, 2013 at 23:00
-
possible duplicate of this questionLuiggi Mendoza– Luiggi Mendoza2013年12月18日 23:05:55 +00:00Commented Dec 18, 2013 at 23:05
2 Answers 2
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.
answered Dec 18, 2013 at 23:05
Tom Leese
19.8k12 gold badges47 silver badges71 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Dingles
I keep getting errors that
ArrayList cannot be resolved to a typeLuiggi Mendoza
@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.Dingles
Ahhh I forgot the
import java.util.Arrays package.
answered Dec 18, 2013 at 23:03
JayD
6,6314 gold badges22 silver badges24 bronze badges
2 Comments
Luiggi Mendoza
Instead of adding an answer pointing to an existing Q/A, flag the question as duplicate.
JayD
will do.. kind of new to SO
lang-java