Julia 如何声明派生类型的数组?(朱莉娅)

Julia 如何声明派生类型的数组?(朱莉娅),julia,Julia,我有一个派生类型,表示为: struct BoundaryCondition domain::MeshStructure left::BorderValue right::BorderValue bottom::BorderValue top::BorderValue back::BorderValue front::BorderValue end 我想创建一个派生类型的数组,大小为n_comp,代码中的数组是: bc = Array{BoundaryConditi

我有一个派生类型,表示为:

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类型的数组的正确方法是什么?

如果要预先分配它,需要用一些东西填充数组。例如,您可以使用
unde
(将
Bool
类型和大小替换为您的问题类型和大小):

您还可以使用
fill
(同样,用问题的默认“fill”值替换
false
):

如果你不需要预先分配,一个好的解决办法就是理解。利用索引功能创建边界条件,例如:

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]
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]