Scala创建空数组数组 [英] Scala create array of empty arrays
问题描述
我正在尝试创建一个数组,其中每个元素都是一个空数组.我试过这个:
I am trying to create an array where each element is an empty array. I have tried this:
var result = Array.fill[Array[Int]](Array.empty[Int])
查看这里 如何创建和在 Scala 中使用多维数组?,我也试过这个:
After looking here How to create and use a multi-dimensional array in Scala?, I also tried this:
var result = Array.ofDim[Array[Int]](Array.empty[Int])
然而,这些都不起作用.
However, none of these work.
如何创建空数组的数组?
How can I create an array of empty arrays?
推荐答案
你在这里误解了 Array.ofDim
.给定维度和要保存的值的类型,它创建一个多维数组.
You are misunderstanding Array.ofDim
here. It creates a multidimensional array given the dimensions and the type of value to hold.
要创建一个由 100 个数组组成的数组,每个数组都是空的(0 个元素)并保存 Int
,您只需将这些维度指定为 ofDim的参数代码>函数.
To create an array of 100 arrays, each of which is empty (0 elements) and would hold Int
s, you need only to specify those dimensions as parameters to the ofDim
function.
val result = Array.ofDim[Int](100, 0)
这篇关于Scala创建空数组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!