var arr = Array[Int](arr_size)
println(arr_size + " " + arr.size)
arr_size
is 30 but arr.size
is 1? Why is this?
I am trying to declare an empty array that I can fill in later at designated indexes.
Array[Int](arr_size)
creates an array with one element, arr_size
, and is commonly written as Array(arr_size)
, assuming arr_size
type is Int
.
It calls apply
method in the Array companion object with the signature:
def apply(x: Int, xs: Int*): Array[Int]
Use this instead:
Array.ofDim[Int](arr_size)
You could also use more functional approach and fill the array directly during initialization, e.g. by Array.tabulate
.
If you really want to call an array constructor directly, you can use:
new Array[Int](arr_size)