StringArrayKotlin中没有任何内容(这是为什么的解释),请Array<String>改用。
如果可以在创建数组时提供数组项,则可以按以下步骤完成创建数组的操作:
val result = Array(10) { i ->
Array(8) { j ->
"the String at position $i, $j" // provide some initial value based on i and j
}
}
println(result[0][3]) // Prints: the String at position 0, 3
Run Code Online (Sandbox Code Playgroud)
否则,您可以使用一些默认String值:
val result = Array(10) { Array(8) { "" } }
Run Code Online (Sandbox Code Playgroud)
或创建填充有null值的内部数组(注意:您将不得不处理可空性,您将无法将这些项目用作非空值):
val result = Array(10) { arrayOfNulls<String>(8) } // The type is `Array<Array<String?>>
result[0][0] = "abc"
println(result[0][0]!!.reversed()) // Without `!!`, the value is treated as not-safe-to-use
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3104 次 |
| 最近记录: |