如何在Kotlin复制列表?

我使用

val selectedSeries = mutableListOf<String>()
selectedSeries.addAll(series)

有没有更简单的方法?


当前回答

I can come up with two alternative ways:

1. val selectedSeries = mutableListOf<String>().apply { addAll(series) }

2. val selectedSeries = mutableListOf(*series.toTypedArray())

更新:使用新的类型推断引擎(在Kotlin 1.3中可选),我们可以省略第一个例子中的泛型类型参数,并得到:

1. val selectedSeries = mutableListOf().apply { addAll(series) }

仅供参考。选择新推理的方法是kotlinc -Xnew-inference ./SourceCode。对于命令行,或者对于Gradle, kotlin {experimental {newInference 'enable'}。有关新的类型推断的更多信息,请查看此视频:KotlinConf 2018 -新的类型推断和相关语言特性,作者:Svetlana Isakova,特别是“30岁的构建器推理”

其他回答

对于简单的列表,上面有许多正确的解决方案。

然而,它只是用于较浅的列表。

下面的函数适用于任何二维数组列表。实际上,ArrayList相当于MutableList。有趣的是,它在使用显式MutableList类型时不起作用。如果一个人需要更多的维度,就需要更多的函数。

fun <T>cloneMatrix(v:ArrayList<ArrayList<T>>):ArrayList<ArrayList<T>>{
  var MatrResult = ArrayList<ArrayList<T>>()
  for (i in v.indices) MatrResult.add(v[i].clone() as ArrayList<T>)
  return MatrResult
}

演示整数矩阵:

var mat = arrayListOf(arrayListOf<Int>(1,2),arrayListOf<Int>(3,12))
var mat2 = ArrayList<ArrayList<Int>>()
mat2 = cloneMatrix<Int>(mat)
mat2[1][1]=5
println(mat[1][1])

它显示了12

就像在Java中一样:

列表:

    val list = mutableListOf("a", "b", "c")
    val list2 = ArrayList(list)

Map:

    val map = mutableMapOf("a" to 1, "b" to 2, "c" to 3)
    val map2 = HashMap(map)

假设你的目标是JVM(或Android);我不确定它是否适用于其他目标,因为它依赖于ArrayList和HashMap的复制构造函数。

You can use the provided extension Iterable.toMutableList() which will provide you with a new list. Unfortunately, as its signature and documentation suggest, it's meant to ensure that an Iterable is a List (just like toString and many other to<type> methods). Nothing guarantees you that it's going to be a new list. For instance, adding the following line at the beginning of the extension: if (this is List) return this is a legitimate performance improvement (if it indeed improves the performance).

另外,由于它的名字,结果代码不是很清楚。

我更喜欢添加我自己的扩展,以确保结果,并创建一个更清晰的代码(就像我们有数组):

fun <T> List<T>.copyOf(): List<T> {
    return mutableListOf<T>().also { it.addAll(this) }
}

fun <T> List<T>.mutableCopyOf(): MutableList<T> {
    return mutableListOf<T>().also { it.addAll(this) }
}

注意,addAll是最快的复制方式,因为它使用本机系统。ArrayList的实现中的arraycopy。

另外,要注意这只会给你一个浅拷贝。

编辑:

你可能想要使用更通用的版本:

fun <T> Collection<T>.copyOf(): Collection<T> {
    return mutableListOf<T>().also { it.addAll(this) }
}

fun <T> Collection<T>.mutableCopyOf(): MutableCollection<T> {
    return mutableListOf<T>().also { it.addAll(this) }
}

你可以使用

工作表 -> 到列表()

数组 -> toArray()

ArrayList -> toArray()

MutableList -> toMutableList()


例子:

val array = arrayListOf("1", "2", "3", "4")

val arrayCopy = array.toArray() // copy array to other array

Log.i("---> array " ,  array?.count().toString())
Log.i("---> arrayCopy " ,  arrayCopy?.count().toString())

array.removeAt(0) // remove first item in array 

Log.i("---> array after remove" ,  array?.count().toString())
Log.i("---> arrayCopy after remove" ,  arrayCopy?.count().toString())

打印日志:

array: 4
arrayCopy: 4
array after remove: 3
arrayCopy after remove: 4

I can come up with two alternative ways:

1. val selectedSeries = mutableListOf<String>().apply { addAll(series) }

2. val selectedSeries = mutableListOf(*series.toTypedArray())

更新:使用新的类型推断引擎(在Kotlin 1.3中可选),我们可以省略第一个例子中的泛型类型参数,并得到:

1. val selectedSeries = mutableListOf().apply { addAll(series) }

仅供参考。选择新推理的方法是kotlinc -Xnew-inference ./SourceCode。对于命令行,或者对于Gradle, kotlin {experimental {newInference 'enable'}。有关新的类型推断的更多信息,请查看此视频:KotlinConf 2018 -新的类型推断和相关语言特性,作者:Svetlana Isakova,特别是“30岁的构建器推理”