android - Kotlin - 将 List 转换为 MutableList 的最惯用方式

标签 android kotlin

我有一个返回 getContacts 的方法 (List<Contact>)我需要将此结果转换为 MutableList<Contact> .目前我能想到的最好方法是这样的:

val contacts: MutableList<Contact> = ArrayList(presenter.getContacts())

有没有更惯用/“更少 Java”的方式来做到这一点?

最佳答案

考虑使用 toMutableList() 功能:

presenter.getContacts().toMutableList()

toMutableList()可能希望转换为可变列表的 stdlib 类型的扩展:Collection<T> , Iterable<T> , Sequence<T> , CharSequence , Array<T>和原始数组。

关于android - Kotlin - 将 List 转换为 MutableList 的最惯用方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40682918/

相关文章:

android - 应用程序不使用全屏宽度(Actionbar)

javascript - react : onClick on Mobile (iPhone) requires 2 taps?

java - Class.forName android NoClassDefFoundError -> ClassNotFoundException

android - 尝试从空数组中读取,但数组包含多个文件名

java - 无限 while 循环,而 AsyncTask 未完成

android - 如何在使用 setSpan 时忽略某些单词或字符

firebase - Firestore 在失去并重新获得互联网连接后停止更新

android - Kotlin 使用 Any is xxx || 调用带参数函数任何 yyy 都不起作用

json - Kotlinx.Serializer - 创建一个快速的 JSON 发送

android - 如何在 Jetpack Compose 的 TextField 中正确设置高度?