You have:
val array = new Array[Array[Cell]](height, width)
How do you initialize all elements to new Cell("something")?
Thanks, Etam (new to Scala).
You have:
val array = new Array[Array[Cell]](height, width)
How do you initialize all elements to new Cell("something")?
Thanks, Etam (new to Scala).
val array = Array.fromFunction((_,_) => new Cell("something"))(height, width)
Array.fromFunction accepts a function which takes n integer arguments and returns the element for the position in the array described by those arguments (i.e. f(x,y) should return the element for array(x)(y)) and then n integers describing the dimensions of the array in a separate argument list.
val array = Array.fill(height)(Array.fill(width)(new Cell("something")))
Welcome to Scala version 2.8.0.r21376-b20100408020204 (Java HotSpot(TM) Client VM, Java 1.6.0_18).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val (height, width) = (10,20)
height: Int = 10
width: Int = 20
scala> val array = Array.fill(height, width){ new Cell("x") }
array: Array[Array[Cell[java.lang.String]]] = Array(Array(Cell(x), Cell(x), ...
scala>
Assuming the array has already been created, you can use this:
for {
i <- array.indices
j <- array(i).indices
} array(i)(j) = new Cell("something")
If you can initialize at creation, see the other answers.