如何在Kotlin复制列表?
我使用
val selectedSeries = mutableListOf<String>()
selectedSeries.addAll(series)
有没有更简单的方法?
如何在Kotlin复制列表?
我使用
val selectedSeries = mutableListOf<String>()
selectedSeries.addAll(series)
有没有更简单的方法?
当前回答
val selectedSeries = listOf(*series.toTypedArray())
其他回答
这很好。
val selectedSeries = series.toMutableList()
你可以使用ArrayList构造函数:
试试下面的代码复制列表在Kotlin
arrayList2.addAll(arrayList1.filterNotNull())
我建议你做个肤浅的拷贝
.map{it}
这将适用于许多收集类型。
以我之见,最好也是最常用的方法是在Kotlin(1.6+)的新版本中使用集合构建器。
val shallowListCopy = buildList { addAll(list) }