【问题标题】:How to convert an inmutable Seq of immutable Seq into a mutable Seq of mutable Seq in Scala?如何在Scala中将不可变Seq的不可变Seq转换为可变Seq的可变Seq?
【发布时间】:2018-04-22 18:45:31
【问题描述】:

设不可变 Seq 的不可变 Seq 为:

val content: Seq[Seq[Double]

我想将其转换为可变 Seq 的可变 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]]]

如何处理?

【问题讨论】:

    标签: scala immutability mutable


    【解决方案1】:

    鉴于这个不可变的输入:

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

    您可以使用 varargs constructor 切换到可变 Seq 的可变 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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-06
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多