I would like to initialize an ArrayBuffer
with value -1 in indexes 0 through 99. Is there a simple, idiomatic way to do so?
This works, but it's a bit crufty:
val a = new ArrayBuffer[Int]()<br>
a.appendAll(Nil.padTo(100, -1))
I'd like to see something more like this:
val a = ArrayBuffer(List(-1) * 100)
Arrays -- An array is a data structure which stores a collection of elements of the same type. Arrays have a fixed size. ArrayBuffer -- An ArrayBuffer is very similar to an array, except it can grow in size after it has been initialized.
Array in scala is homogeneous and mutable, i.e it contains elements of the same data type and its elements can change but the size of array size can't change.
collection.mutable.ArrayBuffer.fill(100)(-1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With