作为this question的后续内容,如何在Julia中创建一个充满副本而非引用的数组?
使用一个例子,在修改数组的元素时,如何创建像fill
这样的函数,其行为类似于a
?
julia> a = [[],[]]
2-element Array{Array{Any,1},1}:
Any[]
Any[]
julia> push!(a[1],1.0)
1-element Array{Any,1}:
1.0
julia> a
2-element Array{Array{Any,1},1}:
Any[1.0]
Any[]
julia> b = fill([],2)
2-element Array{Array{Any,1},1}:
Any[]
Any[]
julia> push!(b[1],1.0)
1-element Array{Any,1}:
1.0
julia> b
2-element Array{Array{Any,1},1}:
Any[1.0]
Any[1.0]
答案 0 :(得分:7)
理解,可以提供帮助:
julia> a=[[] for i=1:4]
4-element Array{Array{Any,1},1}:
Any[]
Any[]
Any[]
Any[]
julia> push!(a[1],1)
1-element Array{Any,1}:
1
julia> a
4-element Array{Array{Any,1},1}:
Any[1]
Any[]
Any[]
Any[]