+ 1
ArrayList val = New Array List(); Collections. ?
I'm a bit confused whether it's Collections.add or addAll?
6 Antworten
+ 2
Okay, the implementation requires either a single element of the list elements type (in your case obviously integer). So you could add a single element via list.add(42).
Or you add multiple elements, therefore the implementation requires a list as parameter. list.addAll(anotherList).
To do this, you need to create a list from your multiple values (4, 2) first. This can be done in many ways.
The easiest beginner-version, which is easy to understand is to create a new list of the same type:
new List...bla
newList.add(4)
newList.add(2)
originalList.addAll(newList)
The confusing way is to use the Collections-implementation to create a new list with the short-way:
originalList.addAll( Collections.asList(4, 2) )
0
If you have a list (like your variable 'val'), then you have to use the corresponding implementation of add (single item) or addAll (multiple) of its type: List.
0
Oh.. so it should be addAll then?
0
Do you want to add a single element or more than one? Single: add, more: addAll.
0
I will be adding val(2, 4, 3);
0
Thank you 💜