Francisco Pizarro
Francisco Pizarro

Reputation: 45

Creating Java-style 2D arrays with second dimension unspecified

In Java I can do something like:

long[][] foo = new long[10][]
foo[0] = new long[1]
foo[1] = new long[2]

How can I do something similar in Scala?

Upvotes: 0

Views: 70

Answers (1)

Andrey Tyukin
Andrey Tyukin

Reputation: 44908

You can use Array.ofDim[X](d) to create array of type X and dimension d:

val foo = Array.ofDim[Array[Long]](10)
foo(0) = Array.ofDim[Long](1)
foo(1) = Array.ofDim[Long](2)

or you can use new:

val foo = new Array[Array[Long]](10)
foo(0) = new Array[Long](1)
foo(1) = new Array[Long](2)

to achieve the same.

Upvotes: 2

Related Questions