PavelOliynyk PavelOliynyk - 1 year ago 86
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 Source

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


Vector.fill(100)(ListBuffer(1, 2, 3))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download