122

我是 Scala 的新手,今天才开始学习它。我想知道如何在 Scala 中初始化一个数组。

示例 Java 代码

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

Scala 中上述代码的等价物是什么?

4

7 回答 7

153
scala> val arr = Array("Hello","World")
arr: Array[java.lang.String] = Array(Hello, World)
于 2010-10-07T11:11:13.143 回答
112

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

于 2012-03-15T11:10:08.130 回答
41

还可以使用填充进行更多动态初始化,例如

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

==>

Array[Int] = Array(0, 1, 0, 0, 3, 2, 4, 1, 4, 3)
于 2012-08-21T08:42:21.797 回答
18

除了 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)
于 2010-10-07T13:54:32.110 回答
16

如果你知道数组的长度但你不知道它的内容,你可以使用

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

如果你想有二维数组但你不知道它的内容,你可以使用

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

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

如果你已经知道它的内容,你可以使用

val temp = Array("a", "b")
于 2014-09-19T18:40:54.690 回答
12

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

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

res3: Array[Array[String]] = Array(Array("", "", ""), Array("", "", ""),Array("", "", ""), Array("", "", ""))
于 2015-01-16T06:15:42.397 回答
1

[合并所有答案]

初始化一维数组

// 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/nD 阵列

// 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...)(functionWhichReturnsTYPE) 否则

输出供参考:

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

于 2021-06-20T12:20:11.790 回答