Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an ArrayBuffer of ArrayBuffer

I want a two-dimensional array of ArrayBuffer

Something like this:

var myRowOfStrings = new ArrayBuffer[String]
val myArrayOfRows = new ArrayBuffer[ArrayBuffer] // To store many ArrayBuffer[String]

But the Scala compiler doesn't like the second declaration:

scala> val myArrayOfRows = new ArrayBuffer[ArrayBuffer]
<console>:8: error: class ArrayBuffer takes type parameters
       val myArrayOfRows = new ArrayBuffer[ArrayBuffer]
                                       ^

Have I got the syntax wrong?

Or is an ArrayBuffer of ArrayBuffer not possible?

like image 515
Koala3 Avatar asked Nov 23 '25 16:11

Koala3


1 Answers

ArrayBuffer objects require a type. It says so in the error message.

You need to tell the compiler what type of ArrayBuffer you want.

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val myArrayOfRows = new ArrayBuffer[ArrayBuffer[String]]
myArrayOfRows: scala.collection.mutable.ArrayBuffer[scala.collection.mutable.ArrayBuffer[String]] = ArrayBuffer()

Consider doing this if its easier.

type Row = ArrayBuffer[String]
var myRowOfStrings = new Row
val myArrayOfRows = new ArrayBuffer[Row]
like image 110
Kyle Avatar answered Nov 25 '25 05:11

Kyle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!