I have input data of type List<UnitWithComponents>

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

I want to convert the data to a vararg of Unit

Currently I am doing *data.map { it.unit!! }.toTypedArray(). Is there a better way to do it?

推荐答案

不,这是正确的方法(假设列表中某个元素的it.unitnull时,您想要抛出一个异常).

Kotlin相关问答推荐

Android Jetpack编写androidx.compose.oundation.lazy.grid.Items

将文本与文本字段的内容对齐

Kotlin-elvis算子don';不使用map.get()

Kotlin 接口类型参数

在kotlin中匹配多个变量

根据字符串值动态过滤数组列表 - kotlin

Kotlin 中的密封和内部有什么区别?

为什么 Kotlin 中的 Double 和 Long 类型不推荐直接转换为 Char?

JavaFX - 你如何在 Kotlin 中使用 MapValueFactory?

Jetpack BottomNavigation - java.lang.IllegalStateException:Already attached to lifecycleOwner

AIDL 中的 Parcelize 注释:Incompatible types: Object cannot be converted to MyCustomObject

如何将超过 2 个 api 调用的结果与 Coroutines Flow 结合起来?

如何解决此错误请Kotlin:[Internal Error] java.lang.ExceptionInInitializerError

如何在 Android Studio 3.1.3 中查看 Kotlin 中有趣的源代码?

如果我可以将 Flow 和 StateFlow 与生命周期范围 \ viewLifecycleOwner.lifecycleScope 一起使用,那么在 ViewModel 中使用 LiveData 有什么意义

在kotlin中,如何模拟封装回调函数?

在调用Kotlin数据类中的超类构造函数之前访问函数

如何在Kotlin中创建填充空值的通用数组?

参数不匹配;SimpleXML

用Gradle Kotlin DSL构建源jar?