Julia检查数组条目是否为#undef的最佳方法是什么?
示例:
julia> a = Array(Vector,2)
julia> isdefined(a[1]) # fails
julia> isempty(a[1]) # fails
答案 0 :(得分:12)
您可以使用isdefined
代替isdefined(a, 1)
将访问权限推送到isdefined(a[1])
:
julia> a = Array(Vector,2);
julia> a[2] = {10}
1-element Array{Any,1}:
10
julia> a
2-element Array{Array{T,1},1}:
#undef
{10}
julia> isdefined(a[1])
ERROR: access to undefined reference
in getindex at array.jl:246
julia> isdefined(a, 1)
false
julia> isdefined(a, 2)
true