Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala array initialization

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).

like image 219
Etam Avatar asked Apr 10 '10 18:04

Etam


People also ask

How to create array of integers in Scala?

Create Array with RangeUse of range() method to generate an array containing a sequence of increasing integers in a given range. You can use final argument as step to create the sequence; if you do not use final argument, then step would be assumed as 1.

How to define array variable in Scala?

Scala arrays can be generic. which mean we can have an Array[T], where T is a type parameter or abstract type. Scala arrays are compatible with Scala sequences – we can pass an Array[T] where a Seq[T] is required. Scala arrays also support all sequence operations.


1 Answers

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>
like image 86
Eastsun Avatar answered Sep 25 '22 00:09

Eastsun