JGu*_*Guo 10 list variadic-functions kotlin
我有类型的输入数据 List<UnitWithComponents>
class UnitWithComponents {
var unit: Unit? = null
var components: List<Component> = ArrayList()
}
Run Code Online (Sandbox Code Playgroud)
我想将数据转换vararg为Unit
目前我正在做*data.map { it.unit!! }.toTypedArray()。有更好的方法吗?
fun foo(vararg strings: String) { /*...*/ }
Run Code Online (Sandbox Code Playgroud)
foo(strings = arrayOf("a", "b", "c"))
val list: MutableList<String> = listOf("a", "b", "c") as MutableList<String>
foo(strings = list.map { it }.toTypedArray())
Run Code Online (Sandbox Code Playgroud)
非 Kotlin 函数 (*.java) 不允许命名参数
因此,在这种情况下,您应该替换:
从: strings = list.map { it }.toTypedArray()
到: *list.map { it }.toTypedArray()
GL
| 归档时间: |
|
| 查看次数: |
2953 次 |
| 最近记录: |