An idiomatic way to initialize a Scala ArrayBuffer array?

I would like to initialize ArrayBufferwith a value of -1 in indices from 0 to 99. Is there a simple, idiomatic way to do this?

This works, but it's a little cool:

val a = new ArrayBuffer [Int] ()
a.appendAll (Nil.padTo (100, -1))

I would like to see something like this:

val a = ArrayBuffer (List (-1) * 100)

+5
source share
1 answer
collection.mutable.ArrayBuffer.fill(100)(-1)
+14
source

All Articles