Seems so simple, but, how do I initialize Kotlin's MutableList
to empty MutableList
?
I could hack it this way, but I'm sure there is something easier available:
var pusta: List<Kolory> = emptyList() var cos: MutableList<Kolory> = pusta.toArrayList()
To create an empty list in Kotlin, we have two ways. One is using listOf() function and the other is using emptyList() function.
You can simply write:
val mutableList = mutableListOf<Kolory>()
This is the most idiomatic way.
Alternative ways are
val mutableList : MutableList<Kolory> = arrayListOf()
or
val mutableList : MutableList<Kolory> = ArrayList()
This is exploiting the fact that java types like ArrayList
are implicitly implementing the type MutableList
via a compiler trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With