Scala 中的数组初始化

发布于 2024-09-26 15:20:07 字数 177 浏览 6 评论 0原文

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

Java 代码示例

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

上述代码在 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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

回心转意 2024-10-03 15:20:07
scala> val arr = Array("Hello","World")
arr: Array[java.lang.String] = Array(Hello, World)
scala> val arr = Array("Hello","World")
arr: Array[java.lang.String] = Array(Hello, World)
太阳男子 2024-10-03 15:20:07

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

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

这相当于 Java 的 new byte[5]

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

一杯敬自由 2024-10-03 15:20:07

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

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

==>

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

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)
沉溺在你眼里的海 2024-10-03 15:20:07

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)

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)
旧时浪漫 2024-10-03 15:20:07

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

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

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")
情丝乱 2024-10-03 15:20:07

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

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

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

Another way of declaring multi-dimentional arrays:

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

res3: Array[Array[String]] = Array(Array("", "", ""), Array("", "", ""),Array("", "", ""), Array("", "", ""))
魂ガ小子 2024-10-03 15:20:07

[合并所有答案]

初始化一维数组

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文