https://kotlinlang.org logo
#android
Title
# android
s

Sam

09/10/2019, 8:49 AM
Hello you guys, So I have a question,Why we can not use
arrayListOf
as parameter , only ArrayList can accept to use as parameter
Copy code
1. class CategoriesViewHolder(context: Context,categories:ArrayList<Category>){}
2. val categories = arrayListOf<String>()
Thanks
g

gergo

09/10/2019, 8:58 AM
I might not get what you mean, but ArrayList is a type while arrayListOf is a function to create an ArrayList. You can use arrayListOf to have a default parameter for your method if that's what you are trying to achieve.
fun foo(bar: ArrayList<Int> = arrayListOf()){}
s

Sam

09/10/2019, 8:59 AM
Screen Shot 2019-09-10 at 3.59.05 PM.png
g

gergo

09/10/2019, 8:59 AM
still, arrayListOf is a function to create an ArrayList, it's not a type
if you want to add any number of elements to the method via commas, then you may want to use vararg
fun foo(vararg bar: String){ bar.forEach { } }
caller side: foo("a","b")
w

wasyl

09/10/2019, 9:03 AM
Henry, it’s the same difference as
listOf<X>
and
List<X>
. Also the same difference as Java’s
List<X>
and
ArrayList<X>()
- the type and the constructor
👍 2
❤️ 1
s

Sam

09/10/2019, 9:44 AM
Thanks you guys :))))