gpt4 book ai didi

list - Kotlin 将 List 转换为可变参数

转载 作者:IT老高 更新时间:2023-10-28 13:42:58 25 4
gpt4 key购买 nike

我有 List<UnitWithComponents> 类型的输入数据

class UnitWithComponents {
var unit: Unit? = null
var components: List<Component> = ArrayList()
}

我想将数据转换为 varargUnit

目前我在做*data.map { it.unit!! }.toTypedArray() .有没有更好的方法?

最佳答案

fun foo(vararg strings: String) { /*...*/ }

使用

foo(strings = arrayOf("a", "b", "c"))

val list: MutableList<String> = listOf("a", "b", "c") as MutableList<String>
foo(strings = list.map { it }.toTypedArray())

Named arguments are not allowed for non-Kotlin functions (*.java)

因此,在这种情况下,您应该替换:

来自:strings = list.map { it }.toTypedArray()

收件人:*list.map { it }.toTypedArray()

GL

Source

关于list - Kotlin 将 List 转换为可变参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51161558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com