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

How to convert an inmutable Seq of immutable Seq into a mutable Seq of mutable Seq in Scala?

提问人:JarsOfJam-Scheduler 提问时间:4/23/2018 最后编辑:Xavier GuihotJarsOfJam-Scheduler 更新时间:4/23/2018 访问量:260

问:

设不可变 Seqs 的不可变 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]]]

如何处理?

斯卡拉 不变性 可变

评论


答:

3赞 Xavier Guihot 4/23/2018 #1

给定此不可变输入:

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

您可以使用 varargs 构造函数切换到可变 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))