This question already has answers here:
Initialization of an ArrayList in one line
(34 answers)
Closed 8 years ago.
Sorry i really dont know how to do this, last resort but to ask.
I wanted to add values to the string list. But my code is not working
private List<String> sample = new ArrayList<String>(){"item1","item2","item3"};
Here it is:
List<String> sample = new ArrayList<String>(Arrays.asList("item1", "item2", "item3"));
Try,
ArrayList<String> list = new ArrayList<String>() {{
add("item1");
add("item2");
add("item3");
}}
OR
ArrayList<String> list = new ArrayList<String>();
list.add("item1");
list.add("item2");
list.add("item3");
Related
This question already has answers here:
Run piece of code contained in a String
(9 answers)
Closed 3 years ago.
List<PhoneBook> list = new ArrayList<PhoneBook>();
String str = "new PhoneBook("David","234567");";
How can I add (or convert) str to code and add it to the list?
I have searched all over the internet, and didn't find anything helpful.
To convert it to fit the arraylist you've given:
List<PhoneBook> list = new ArrayList<>();
PhoneBook not_a_str = new PhoneBook("David","234567");
list.add(not_a_str);
To add strings to a list of objects:
List<Object> list = new ArrayList<>();
String str = "new PhoneBook(\"David\",\"234567\")";
list.add(str);
To add strings to a list of strings (also objects):
List<String> list = new ArrayList<>();
String str = "new PhoneBook(\"David\",\"234567\")";
list.add(str);
list.add("Anything else");
Good luck with java
This question already has answers here:
Initialization of an ArrayList in one line
(34 answers)
Closed 7 years ago.
I would like to write a linked list like this:
"a" -> "b" -> "c" -> "d"
This is what I've tried so far but it's obviously wrong. I was wondering how to express this correctly in java?
LinkedList<String> s = new LinkedList<>();
s = {"a"->"b"->"c"->"d"};
Thanks!
That's how the pointers in the list look internally, to actually add it to the list you need to do this:
List<String> s = new LinkedList<>();
s.add("a");
s.add("b");
s.add("c");
s.add("d");
Take a look at this answer.
LinkedList<String> list = new LinkedList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
If you really want it on one line:
LinkedList<String> list = new LinkedList<>(Arrays.asList("a","b","c","d"));
Though that does have a performance overhead.
You could do this:
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("a");
linkedList.add("b");
linkedList.add("c");
linkedList.add("d");
This question already has an answer here:
Difference between new ArrayList<String>() and new ArrayList<>() [duplicate]
(1 answer)
Closed 8 years ago.
What is the difference between
List<Object> list = new ArrayList<Object>();
and
List<Object> list = new ArrayList<>();
They both compile.
There's no difference. The latter is a shortcut introduced in Java 7.
This question already has answers here:
Create ArrayList from array
(42 answers)
Closed 9 years ago.
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
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 here
new ArrayList<Element>(Arrays.asList(array))
*** new
This question already has answers here:
What is the point of the diamond operator (<>) in Java?
(7 answers)
Closed 9 years ago.
What would be the difference between these two lines effectively?
ArrayList <MyClass> arrayList = new ArrayList<>();
and
ArrayList <MyClass> arrayList = new ArrayList<MyClass>();
ArrayList <MyClass> arrayList = new ArrayList<>();
This one is valid in Java7.
ArrayList <MyClass> arrayList = new ArrayList<MyClass>();
This one is valid in Java7, Java6 and Java5.