我刚接触 Kotlin,对于 init 函数 在数组上下文中的作用理解有困难。具体来说,如果我想创建一个 String
类型的数组:
val a = Array<String>(a_size){"n = $it"}
这个代码可行,但是
"n = $it"
是什么意思呢?它不像init
函数,因为它在大括号内而不是括号内。如果我想要一个
Int
数组,init
函数或大括号内的内容应该怎样写?
/**
* Creates a new array with the specified [size], where each element is calculated by calling the specified
* [init] function. The [init] function returns an array element given its index.
*/
public inline constructor(size: Int, init: (Int) -> T)
a
的结果。[
"n = 0",
"n = 1",
...,
"n = $a_size"
]
0
的数组,可以这样做:val a = Array<Int>(a_size) { 0 }
另外,您可以按照以下方式创建数组:
val a = arrayOf("a", "b", "c")
val b = intArrayOf(1, 2, 3)
ArrayList<Integer> lst = new ArrayList<Integer>(10);
的语句是什么? - ArafArray<{{TYPE}}>(a_size) { {{GENERAL_INITIALIZER}} }
的读取方式。当前的语法是否支持这种操作? - Araf