This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Creating a multi-dimensional array

1 reply
Robert Kosara
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
I'm sure this is something trivial, but I can't seem to create a multi-dimensional array. The scala.Array class has constructors for them, but it won't let me do it:

scala> val data = new Array[Float](4, 4) 
<console>:4: error: too many arguments for array constructor: found 2 but array has only 1 dimension(s)
       val data = new Array[Float](4, 4)
                  ^

What am I doing wrong?

Eastsun 2
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Creating a multi-dimensional array

You can do it like this:

val data = new Array[Array[Float]](4, 4)

Robert Kosara-2 wrote:
>
> I'm sure this is something trivial, but I can't seem to create a
> multi-dimensional array. The scala.Array class has constructors for them,
> but it won't let me do it:
>
> scala> val data = new Array[Float](4, 4)
> :4: error: too many arguments for array constructor: found 2 but
> array has only 1 dimension(s)
> val data = new Array[Float](4, 4)
> ^
>
> What am I doing wrong?
>
>

-----
My scala solutions for http://projecteuler.net/ Project Euler problems:
http://eastsun.javaeye.com/category/34059 Click here

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland