Hello you guys, So I have a question,Why we can no...
# android
s
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
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
Screen Shot 2019-09-10 at 3.59.05 PM.png
g
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
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
Thanks you guys :))))