What would you like a[1,1]
to return here? Perhaps it’s simplest to figure out what’s going on without any undef
stuff:
julia> [fill(i+10j, 2,3) for i=1:2, j=1:3]
2×3 Array{Array{Int64,2},2}:
[11 11 11; 11 11 11] [21 21 21; 21 21 21] [31 31 31; 31 31 31]
[12 12 12; 12 12 12] [22 22 22; 22 22 22] [32 32 32; 32 32 32]
julia> a[2,3]
2×3 Array{Int64,2}:
32 32 32
32 32 32
julia> a[2,3][1,1]
32
Arrays store numbers densly in memory, so an array of undef
just contains whatever junk happened to be in memory, interpreted as numbers, and you can index it just fine.
But an Array of Arrays stores a list of pointers, and if this is undef
, there is no object to return when you index.
julia> Vector{Float64}(undef, 3)
3-element Array{Float64,1}:
2.335323258e-314
2.245962545e-314
2.3353232737e-314
julia> ans[1]
2.335323258e-314
julia> [Vector{Int}(undef, 3) for _=1:3]
3-element Array{Array{Int64,1},1}:
[4944151024, 4551525968, 4975935952]
[0, 4294967296, 0]
[4610370144, 4476805312, 4547078800]
julia> ans[1]
3-element Array{Int64,1}:
4944151024
4551525968
4975935952
julia> v = Vector{Vector{Int}}(undef, 3)
3-element Array{Array{Int64,1},1}:
#undef
#undef
#undef
julia> ans[1]
ERROR: UndefRefError: access to undefined reference
julia> v[2] = [2,3,4,5,6,7,8];
julia> v
3-element Array{Array{Int64,1},1}:
#undef
[2, 3, 4, 5, 6, 7, 8]
#undef
julia> v[3] = rand(3,3)
ERROR: MethodError: no method matching Array{Int64,1}(::Array{Float64,2})