如何使用until循环将不可变的Seq转换为可变的seq

rey*_*n64 10 scala mutable immutability sequence

我试图返回一个带有until循环的可变序列,但是我有一个不可变的seq返回(0到nbGenomes):

 def generateRandomGenome(nbGenomes:Int): IndexedSeq[GenomeDouble]={
    return ((0 until nbGenomes toSeq).map{e => generateRandomGenome}) 
  }
Run Code Online (Sandbox Code Playgroud)

返回编译错误:

found   : scala.collection.immutable.IndexedSeq[org.openmole.tools.mgo.mappedgenome.genomedouble.GenomeDouble]
 required: scala.collection.mutable.IndexedSeq[org.openmole.tools.mgo.mappedgenome.genomedouble.GenomeDouble]
    return ((0 until nbGenomes toSeq).map{e => generateRandomGenome}) 
Run Code Online (Sandbox Code Playgroud)

我如何强制until循环返回一个可变的seq?谢谢scala社区!

Kim*_*bel 12

您可以通过使用varargs构造函数创建新的可变序列,将不可变序列转换为可变序列.

scala> val l = List(1,2,3)
l: List[Int] = List(1, 2, 3)
scala> scala.collection.mutable.ArraySeq(l:_*)
res0: scala.collection.mutable.ArraySeq[Int] = ArraySeq(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)