How to declare an 1-dim Array of 310 1-dimensional Arrays with Int32 elements

I’m trying to declare a 1-dimensional Array of 310 1-dimensional
arrays with an unspecified number of Int32 elements.

I thought Array{Array{Int32}}(310) was correct (at least circa 2017),
but it fails.

You have to use undef now:

 Array{Array{Int32}}(undef, 310) 

Edit: Also you should use Vector instead of Array. Array{T} is still abstract because it doesn’t specify the dimension. Vector{T} is an alias for Array{T, 1}

2 Likes

Tom,

Thanks for the ‘undef’ info–I doubt that I would ever have found that on my own.

I also saw your
Edit: Also you should use Vector instead of Array. Array{T} is still abstract because it doesn’t specify the dimension. Vector{T} is an alias for Array{T, 1}

While the declaration seems fine–no errors when executing it, everything I try to do after that fails:

If I define P=Vector{Vector{Int32}}(undef,310), how do I refer to each of the 310 vectors?
I would have thought P[1] would be the first of the 310 vectors, but apparently not.

I need to initialize the first two elements of each of the 310 vectors to zero. I would have thought
that P[1][1] and P[1][2] would be the first two elements of the first vector, but apparently not.
I can’t set them to zero.

After I get the first two elements of each of the 310 vectors defined to be zero, I plan to define subsequent
elements via push!..but it looks like that won’t work either:

VectorVector.jl Trying to make push! work with a vector of vectors.

P = Vector{Vector{Int32}}(undef,310)
push!(P[1],1999) # Pushes the integer 1999 into the first open position of vector ‘P[1]’, i.e. P[1][1].
push!(P[1],2020) # Pushes the integer 2020 into the first open position of vector ‘P[1]’, i.e. P[1][2].
println(‘P[1][1]=’,P[1][1])
println(‘P[1][2]=’,P[1[[2])

c:>j vectorvector
ERROR: LoadError: UndefRefError: access to undefined reference
Stacktrace:
[1] getindex(::Array{Array{Int32,1},1}, ::Int64) at .\array.jl:809
[2] top-level scope at j:vectorvector.jl:4
[3] include(::Function, ::Module, ::String) at .\Base.jl:380
[4] include(::Module, ::String) at .\Base.jl:368
[5] exec_options(::Base.JLOptions) at .\client.jl:296
[6] _start() at .\client.jl:506
in expression starting at j:vectorvector.jl:4

c:>

What am I doing wrong?

Are you working in the REPL? If not, I highly suggest centering your workflow around it.

Maybe this can shed some light on what’s going on:

julia> P = Vector{Vector{Int32}}(undef, 3)
3-element Array{Array{Int32,1},1}:
 #undef
 #undef
 #undef

julia> P[1]
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getindex(::Array{Array{Int32,1},1}, ::Int64) at ./array.jl:809
 [2] top-level scope at REPL[181]:1

julia> P[1] = [];

julia> P
3-element Array{Array{Int32,1},1}:
    []
 #undef
 #undef

The array initially contains undefined references (that’s what undef does). If you want to make an array of empty arrays, you can use a comprehension like

[Int32[] for i in 1:310]

Feel free to ask for any further clarification.

3 Likes