在 Julia 中将元素附加到数组的工作方式如下:
v = Array{Int32, 1}(0)
append!(v, 1)
append!(v, 2)
println(v) # prints: Int32[1,2]
当我尝试使用自定义类型时
type Node
label::String
value::Int32
end
nodes = Array{Node, 1}(0)
append!(nodes, Node("a", 42))
我收到以下错误:
ERROR: LoadError: MethodError: no method matching length(::Node)
我认为我必须“实施”length
有方法但不知道如何。
The append!
命令不会执行您认为它执行的操作。你正在考虑push!
命令。
The append!
命令appends两个数组在一起。两个参数都必须是数组:
julia> append!(nodes, [Node("a", 42)])
1-element Array{Node,1}:
Node("a",42)
No length
implementing necessary
(that error was just telling you it tried to read the length of your array for the second argument and found something that was not an array.)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)