Array initializing in Scala
Asked Answered
F

7

124

I'm new to Scala ,just started learning it today.I would like to know how to initialize an array in Scala.

Example Java code

String[] arr = { "Hello", "World" };

What is the equivalent of the above code in Scala ?

Foin answered 7/10, 2010 at 11:8 Comment(0)
B
154
scala> val arr = Array("Hello","World")
arr: Array[java.lang.String] = Array(Hello, World)
Byram answered 7/10, 2010 at 11:11 Comment(1)
This answer doesn't yet explain how to initialize multidimensional arrays in Scala (which is addressed here: #13863068)Lizzielizzy
O
114

To initialize an array filled with zeros, you can use:

> Array.fill[Byte](5)(0)
Array(0, 0, 0, 0, 0)

This is equivalent to Java's new byte[5].

Orgell answered 15/3, 2012 at 11:10 Comment(1)
Just FYI, List as an equivalent initializer List.fill(5)(0), accepts even functions. List.fill(5)(myFunc())Mickel
S
41

Can also do more dynamic inits with fill, e.g.

Array.fill(10){scala.util.Random.nextInt(5)} 

==>

Array[Int] = Array(0, 1, 0, 0, 3, 2, 4, 1, 4, 3)
Supposing answered 21/8, 2012 at 8:42 Comment(0)
G
18

Additional to Vasil's answer: If you have the values given as a Scala collection, you can write

val list = List(1,2,3,4,5)
val arr = Array[Int](list:_*)
println(arr.mkString)

But usually the toArray method is more handy:

val list = List(1,2,3,4,5)
val arr = list.toArray
println(arr.mkString)
Germinate answered 7/10, 2010 at 13:54 Comment(0)
E
16

If you know Array's length but you don't know its content, you can use

val length = 5
val temp = Array.ofDim[String](length)

If you want to have two dimensions array but you don't know its content, you can use

val row = 5
val column = 3
val temp = Array.ofDim[String](row, column)

Of course, you can change String to other type.

If you already know its content, you can use

val temp = Array("a", "b")
Evelyne answered 19/9, 2014 at 18:40 Comment(1)
Also, and the reason I was looking, I saw this Java-like way: new Array[Int](length)Rockie
W
12

Another way of declaring multi-dimentional arrays:

Array.fill(4,3)("")

res3: Array[Array[String]] = Array(Array("", "", ""), Array("", "", ""),Array("", "", ""), Array("", "", ""))
Welch answered 16/1, 2015 at 6:15 Comment(0)
A
2

[Consolidating all the answers]

Initializing 1-D Arrays

// With fixed values
val arr = Array("a", "ab", "c")

// With zero value of the type
val size = 13
val arrWithZeroVal = Array.ofDim[Int](size) //values = 0
val arrBoolWithZeroVal = Array.ofDim[Boolean](size) //values = false

// With default value
val defVal = -1
val arrWithDefVals = Array.fill[Int](size)(defVal)

//With random values
val rand = scala.util.Random
def randomNumberGen: Int = rand.nextInt(5)
val arrWithRandomVals = Array.fill[Int](size){randomNumberGen}

Initializing 2-D/3-D/n-D Arrays

// With zero value of the type
val arr3dWithZeroVal = Array.ofDim[Int](5, 4, 3) 

// With default value
val defVal = -1
val arr3dWithDefVal = Array.fill[Int](5, 4, 3)(defVal)

//With random values
val arr3dWithRandomValv = Array.fill[Int](5, 4, 3){randomNumberGen}

Conclusion : Use Array.ofDim[TYPE](d1, d2, d3...) to use zero value of the type. Use Array.fill[TYPE](d1, d2, d3...)(functionWhichReturnsTYPE) otherwise

Output for reference :

scala> val arr = Array("a", "ab", "c")
arr: Array[String] = Array(a, ab, c)

scala> val size = 13
size: Int = 13

scala> val arrWithZeroVal = Array.ofDim[Int](size) //values = 0
arrWithZeroVal: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

scala> val arrBoolWithZeroVal = Array.ofDim[Boolean](size) //values = false
arrBoolWithZeroVal: Array[Boolean] = Array(false, false, false, false, false, false, false, false, false, false, false, false, false)

scala> val defVal = -1
defVal: Int = -1

scala> val arrWithDefVals = Array.fill[Int](size)(defVal)
arrWithDefVals: Array[Int] = Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1)

scala> val rand = scala.util.Random
rand: util.Random.type = scala.util.Random$@6e3dd5ce

scala> def randomNumberGen: Int = rand.nextInt(5)
randomNumberGen: Int

scala> val arrWithRandomVals = Array.fill[Int](size){randomNumberGen}
arrWithRandomVals: Array[Int] = Array(2, 2, 3, 1, 1, 3, 3, 3, 2, 3, 2, 2, 0)

scala> val arr3dWithZeroVal = Array.ofDim[Int](5, 4, 3) 
arr3dWithZeroVal: Array[Array[Array[Int]]] = Array(Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)), Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)), Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)), Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)), Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)))

scala> val arr3dWithDefVal = Array.fill[Int](5, 4, 3)(defVal)
arr3dWithDefVal: Array[Array[Array[Int]]] = Array(Array(Array(-1, -1, -1), Array(-1, -1, -1), Array(-1, -1, -1), Array(-1, -1, -1)), Array(Array(-1, -1, -1), Array(-1, -1, -1), Array(-1, -1, -1), Array(-1, -1, -1)), Array(Array(-1, -1, -1), Array(-1, -1, -1), Array(-1, -1, -1), Array(-1, -1, -1)), Array(Array(-1, -1, -1), Array(-1, -1, -1), Array(-1, -1, -1), Array(-1, -1, -1)), Array(Array(-1, -1, -1), Array(-1, -1, -1), Array(-1, -1, -1), Array(-1, -1, -1)))

scala> val arr3dWithRandomVals = Array.fill[Int](5, 4, 3){randomNumberGen}
arr3dWithRandomVals: Array[Array[Array[Int]]] = Array(Array(Array(2, 0, 0), Array(4, 1, 0), Array(4, 0, 0), Array(0, 0, 1)), Array(Array(0, 1, 2), Array(2, 0, 2), Array(0, 4, 2), Array(0, 4, 2)), Array(Array(4, 3, 0), Array(2, 2, 4), Array(4, 0, 4), Array(4, 2, 1)), Array(Array(0, 3, 3), Array(0, 0, 4), Array(4, 1, 3), Array(2, 2, 3)), Array(Array(0, 2, 3), Array(1, 4, 1), Array(1, 3, 3), Array(0, 0, 3)))

Afterheat answered 20/6, 2021 at 12:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.