Take the 2-minute tour ×

I am trying to wrote a program to manage a Database through a Scala Gui, and have been running into alot of trouble formatting my data in such a way as to input it into a Table and have the Column Headers populate. To do this, I have been told I would need to use an Array[Array[Any]] instead of an ArrayBuffer[ArrayBuffer[String]] as I have been using.

My problem is that the way I am trying to fill these arrays is modular: I am trying to use the same function to draw from different tables in a MySQL database, each of which has a different number of columns and entries.

I have been able to (I think) define a 2-D array with

val Data = new Array[Array[String]](numColumns)(numRows)

but I haven't found any ways of editing individual cells in this new array.

Data(i)(j)=Value  //or

do not work, and give me errors about "Update" functionality

I am sure this can't possibly be as complicated as I have been making it, so what is the easy way of managing these things in this language?

share|improve this question
add comment

4 Answers

You don't need to read your data into an Array of Arrays - you just need to convert it to that format when you feed it to the Table constuctor - which is easy, as demonstrated my answer to your other question: How do I configure the Column names in a Scala Table?

If you're creating a 2D array, the idiom you want is

val data = Array.ofDim[String](numColumms, numRows)

(There is also new Array[String](numColumns, numRows), but that's deprecated.)

You access element (i, j) of an Array data with data(i)(j) (remember they start from 0).

But in general you should avoid mutable collections (like Array, ArrayBuffer) unless there's a good reason. Try Vector instead.

Without knowing the format in which you're retrieving data from the database it's not possible to say how to put it into a collection.


You can alternatively put the type information on the left hand side, so the following are equivalent (decide for yourself which you prefer):

val a: Array[Array[String]] = Array.ofDim(2,2)
val a = Array.ofDim[String](2,2)

To explain the syntax for accessing / updating elements: as in Java, a multi-dimensional array is just an array of arrays. So here, a(i) is element i of a, which an Array[String], and so a(i)(j) is element j of that array, which is a String.

share|improve this answer
add comment

Luigi's answer is great, but I'd like to shed some light on why your code isn't working.


does not do what you expect it to do. The new Array[Array[String]](numColumns) part does create an array of array of strings with numColumns entries, with all entries (arrys of strings) being null, and returns it. The following (numRows) then just calls the apply function on that returned object, which returns the numRowsth entry in that list, which is null.

You can try that out in the scala REPL: When you input

new Array[Array[String]](10)(9)

you get this as output:

res0: Array[String] = null

Luigi's solution, instead


does the right thing:

res1: Array[Array[String]] = Array(Array(null, null), Array(null, null))
share|improve this answer
add comment

It's rather ugly, but you can update a multidimensional array with update

> val data = Array.ofDim[String](2,2)
> data(0).update(0, "foo")
> data
data: Array[Array[String]] = Array(Array(foo, null), Array(null, null))

Not sure about the efficiency of this technique.

share|improve this answer
add comment

Luigi's answer is great, but I just wanted to point out another way of initialising an Array that is more idiomatic/functional – using tabulate. This takes a function that takes the array cell coordinates as input and produces the cell value:

scala> Array.tabulate[String](4, 4) _
res0: (Int, Int) => String => Array[Array[String]] = <function1>

scala> val data = Array.tabulate(4, 4) {case (x, y) => x * y }
data: Array[Array[Int]] = Array(Array(0, 0, 0, 0), Array(0, 1, 2, 3), Array(0, 2, 4, 6), Array(0, 3, 6, 9))
share|improve this answer
add comment

Your Answer


