What's the Kotlin equivalent of Java's String[]?

后端 未结 8 1194
感情败类
感情败类 2020-12-23 08:26

I see that Kotlin has ByteArray, ShortArray, IntArray, CharArray, DoubleArray, FloatArray, which are equivalent to byte[], short[], int[],char[], double[]

8条回答
  •  时光说笑
    2020-12-23 09:06

    To create an empty Array of Strings in Kotlin you should use one of the following six approaches:

    First approach:

    val empty = arrayOf()
    

    Second approach:

    val empty = arrayOf("","","")
    

    Third approach:

    val empty = Array(3) { null }
    

    Fourth approach:

    val empty = arrayOfNulls(3)
    

    Fifth approach:

    val empty = Array(3) { "it = $it" }
    

    Sixth approach:

    val empty = Array(0, { _ -> "" })
    

提交回复
热议问题