我有一个返回 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/