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

后端 未结 8 1229
感情败类
感情败类 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:10

    Some of the common ways to create a String array are

    1. var arr = Array(5){""}

    This will create an array of 5 strings with initial values to be empty string.

    1. var arr = arrayOfNulls(5)

    This will create an array of size 5 with initial values to be null. You can use String data to modify the array.

    1. var arr = arrayOf("zero", "one", "two", "three")

    When you know the contents of array already then you can initialise the array directly.

    1. There is an easy way for creating an multi dimensional array of strings as well.

      var matrix = Array(5){Array(6) {""}}

      This is how you can create a matrix with 5 rows and 6 columns with initial values of empty string.

提交回复
热议问题