How to create a generic array filled with nulls in Kotlin?

前端 未结 4 943
太阳男子
太阳男子 2021-02-14 02:07

I tried this, and the code didn\'t compile.

class GenericClass() {
    private var arr : Array? = null

    {
        arr = Array(10,          


        
4条回答
  •  粉色の甜心
    2021-02-14 03:07

    You could use a helper function as below:

    @Suppress("UNCHECKED_CAST")
    fun  genericArrayOfNulls(size: Int): Array {
        return arrayOfNulls(size) as Array
    }
    

提交回复
热议问题