Ben Chuanlong Du's Blog

It is never too late to learn.

Array in Scala

Construct an element with initial values.

In [1]:
Array(1, 2, 3)
Out[1]:
res0: Array[Int] = Array(1, 2, 3)
In [2]:
new Array[String](3)
Out[2]:
res1: Array[String] = Array(null, null, null)

Construct an array of given size with all zeros.

In [3]:
Array.fill(3){0}
Out[3]:
res2: Array[Int] = Array(0, 0, 0)
In [4]:
new Array[Int](3)
Out[4]:
res3: Array[Int] = Array(0, 0, 0)

Comments