2

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?

2 Answers 2

11

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]
Sign up to request clarification or add additional context in comments.

Comments

0

Import ArrayBuffer before applying it:

import scala.collection.mutable.ArrayBuffer
var e = ArrayBuffer("a", "b", "c")
scala> e: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(a, b, c)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.