如何在 Scala 中将不可变 Seq 的不可变 Seq 转换为可变 Seq 的可变 Seq?
How to convert an inmutable Seq of immutable Seq into a mutable Seq of mutable Seq in Scala?
设不可变序列的不可变序列为:
val content: Seq[Seq[Double]
我想将其转换为可变序列的可变序列:
val mutable_being_inversed_matrix:
collection.mutable.Seq[collection.mutable.Seq[Double]] =
content.to[collection.mutable.Seq[collection.mutable.Seq[Double]]
但这会产生以下错误:
Error:(79, 128)
scala.collection.mutable.Seq[scala.collection.mutable.Seq[Double]]
takes no type parameters, expected: one
val mutable_being_inversed_matrix: collection.mutable.Seq[collection.mutable.Seq[Double]] = content.to[collection.mutable.Seq[collection.mutable.Seq[Double]]]
如何处理?
鉴于此不可变输入:
val immutableInput = Seq(Seq(1, 2), Seq(4))
您可以使用 varargs constructor:
切换到可变序列的可变序列
scala.collection.mutable.Seq(
immutableInput.map(imseq => scala.collection.mutable.Seq(imseq:_*)):_*
)
产生:
res0: scala.collection.mutable.Seq[scala.collection.mutable.Seq[Int]] =
ArrayBuffer(ArrayBuffer(1, 2), ArrayBuffer(4))
设不可变序列的不可变序列为:
val content: Seq[Seq[Double]
我想将其转换为可变序列的可变序列:
val mutable_being_inversed_matrix:
collection.mutable.Seq[collection.mutable.Seq[Double]] =
content.to[collection.mutable.Seq[collection.mutable.Seq[Double]]
但这会产生以下错误:
Error:(79, 128)
scala.collection.mutable.Seq[scala.collection.mutable.Seq[Double]]
takes no type parameters, expected: one
val mutable_being_inversed_matrix: collection.mutable.Seq[collection.mutable.Seq[Double]] = content.to[collection.mutable.Seq[collection.mutable.Seq[Double]]]
如何处理?
鉴于此不可变输入:
val immutableInput = Seq(Seq(1, 2), Seq(4))
您可以使用 varargs constructor:
切换到可变序列的可变序列scala.collection.mutable.Seq(
immutableInput.map(imseq => scala.collection.mutable.Seq(imseq:_*)):_*
)
产生:
res0: scala.collection.mutable.Seq[scala.collection.mutable.Seq[Int]] =
ArrayBuffer(ArrayBuffer(1, 2), ArrayBuffer(4))