kotlin - Kotlin中*操作的作用是什么?

标签 kotlin

代码A来自官方样本project .

我不明白 valtasks = Remember { mutableStateListOf(*allTask​​s) } 是什么意思,你能告诉我吗?

顺便说一句,Android Studio给我一些信息,你可以看图片A

代码A

@Composable
fun Home() {
    // String resources.
    val allTasks = stringArrayResource(R.array.tasks)
    val allTopics = stringArrayResource(R.array.topics).toList()

    // The currently selected tab.
    var tabPage by remember { mutableStateOf(TabPage.Home) }

    // True if the whether data is currently loading.
    var weatherLoading by remember { mutableStateOf(false) }

    // Holds all the tasks currently shown on the task list.
    val tasks = remember { mutableStateListOf(*allTasks) }

    ...
}

图像A

enter image description here

最佳答案

来自the documentation of varargs :

When you call a vararg -function, you can pass arguments individually, for example asList(1, 2, 3). If you already have an array and want to pass its contents to the function, use the spread operator (prefix the array with *):

val a = arrayOf(1, 2, 3)
val list = asList(-1, 0, *a, 4)

如您所见,它将数组扩展为多个值以在可变参数中使用。如果您有一个包含元素 1, 2, 3 的数组,则可以将 *yourArray 传递给相当于 yourMethod(1,2,3 )

关于kotlin - Kotlin中*操作的作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70242785/

相关文章:

java - Kotlin 解构变量不适用于 Pair、Triple 或数据类

android - espresso 测试超时,测试未运行

kotlin - 用协变类型的 lambda 替换 SAM 构造函数

android - 使用 sortedBy(...) 排序在 kotlin 中不起作用

android - java.lang.RuntimeException:无法在单元测试中创建ViewModel类的实例

java - 如何在 jna 中实现具有联合类型的结构体 sizeof()

java - Kotlin AWS Rekognition 转换

java - 循环遍历中等大小的数据集时,Kotlin 出现内存不足错误

使用workmanager的Android定期位置更新

Android Kotlin 改造协程请求出现 moshi 错误