Array initializing in Scala

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 ?

164829 次浏览
scala> val arr = Array("Hello","World")
arr: Array[java.lang.String] = Array(Hello, World)

Vasil 的回答是: 如果你有一个 Scala 集合给出的值,你可以写

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

但是通常 toArray 方法更方便:

val list = List(1,2,3,4,5)
val arr = list.toArray
println(arr.mkString)

若要初始化填充了零的数组,可以使用:

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

这相当于 Java 的 new byte[5]

也可以使用 fill 做更多的动态 inits,例如。

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

==>

Array[Int] = Array(0, 1, 0, 0, 3, 2, 4, 1, 4, 3)

如果知道 Array 的长度但不知道其内容,则可以使用

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)

当然,您可以将 String 更改为其他类型。

如果您已经知道其内容,则可以使用

val temp = Array("a", "b")

声明多维数组的另一种方法是:

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


res3: Array[Array[String]] = Array(Array("", "", ""), Array("", "", ""),Array("", "", ""), Array("", "", ""))

[巩固所有的答案]

初始化一维数组

// 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}

初始化2-D/3-D/n-D 阵列

// 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}


结论: 使用 Array.ofDim [ TYPE ](d1,d2,d3...)可以使用类型的零值。 否则使用 Array.fill [ TYPE ](d1,d2,d3...)(function WhichReturn nsTYPE)

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