How do I initialize Kotlin's MutableList to empty MutableList?

后端 未结 3 2047
一整个雨季
一整个雨季 2020-12-02 05:29

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 some

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-02 05:48

    I do like below to :

    var book: MutableList = mutableListOf()
    

    /** Returns a new [MutableList] with the given elements. */

    public fun  mutableListOf(vararg elements: T): MutableList
        = if (elements.size == 0) ArrayList() else ArrayList(ArrayAsCollection(elements, isVarargs = true))
    

提交回复
热议问题