PavelOliynyk PavelOliynyk - 1 month ago 7
Scala Question

Variable number of lists in the vector

trying to create variable number of list in the Vector:

val elems = new ListBuffer[String]()
val v = Vector[ListBuffer[String]](elems)


Can I specify on creation some kind of the range 0..100 so next number of empty ists will be created in the corresponding vector?

PS. I did try generators but getting a syntax error:

val gen = for(i <- 0 to 100) yield new ListBuffer[String]()

Answer

You can use Vector.fill to create a filled Vector. Use List instead of ListBuffer. List is immutable and ListBuffer is mutable

Vector.fill(100)(List(1, 2))

or

Vector.fill(100)(ListBuffer(1, 2, 3))
Comments