我有一个派生类型:
struct BoundaryCondition
domain::MeshStructure
left::BorderValue
right::BorderValue
bottom::BorderValue
top::BorderValue
back::BorderValue
front::BorderValue
end
我想创建一个这种派生类型的数组,大小为 n_comp,在我的代码中为:
bc = Array{BoundaryCondition}(n_comp)
for i in 1:n_comp
bc[i] = createBC(m)
bc[i].left.a[:] = 0.0
bc[i].left.b[:] = 1.0
bc[i].left.c[:] = c_left[i]
end
但是我收到一个错误:
ERROR: LoadError: MethodError: no method matching (Array{BoundaryCondition, N} where N)(::Int64)
声明 BoundaryCondition 类型数组的正确方法是什么?
最佳答案
如果你想预分配它,你需要用一些东西填充你的数组。例如,您可以使用 undef
(将 Bool
类型和大小替换为您的问题):
julia> Vector{Bool}(undef, 3)
3-element Vector{Bool}:
1
0
0
您还可以使用 fill
(同样,将 false
替换为问题的默认“fill”值):
julia> fill(false, 3)
3-element Vector{Bool}:
0
0
0
如果您不需要预分配,一个好的解决方案是理解。为创建边界条件创建索引函数,例如:
function my_BC(i)
out = createBC(m)
out.left.a[:] = 0.0
out.left.b[:] = 1.0
out.left.c[:] = c_left[i]
return out
end
然后简单地做
[my_BC(i) for i in 1:n_comp]
关于julia - 如何声明派生类型的数组? ( Julia ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67513000/