+ 1

ArrayList val = New Array List(); Collections. ?

I'm a bit confused whether it's Collections.add or addAll?

18th Jun 2020, 11:31 AM
Bare Bears
Bare Bears - avatar
6 ответов
+ 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) )
18th Jun 2020, 11:56 AM
Sandra Meyer
Sandra Meyer - avatar
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.
18th Jun 2020, 11:36 AM
Sandra Meyer
Sandra Meyer - avatar
0
Oh.. so it should be addAll then?
18th Jun 2020, 11:42 AM
Bare Bears
Bare Bears - avatar
0
Do you want to add a single element or more than one? Single: add, more: addAll.
18th Jun 2020, 11:47 AM
Sandra Meyer
Sandra Meyer - avatar
0
I will be adding val(2, 4, 3);
18th Jun 2020, 11:49 AM
Bare Bears
Bare Bears - avatar
0
Thank you 💜
18th Jun 2020, 6:13 PM
Bare Bears
Bare Bears - avatar