如何将不可变Seq的不可变Seq转换为Scala中可变Seq的可变Seq?

时间:2018-04-22 18:45:31

标签: scala immutability mutable

让不可变Seq的不可变Seq为:

val content: Seq[Seq[Double]

我希望转换为可变Seqs的可变Seq:

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]]]

如何处理?

1 个答案:

答案 0 :(得分:2)

鉴于这种不可变的输入:

val immutableInput = Seq(Seq(1, 2), Seq(4))

你可以使用varargs constructor切换到可变Seqs的可变Seq:

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))