我正在尝试将字符串添加到数组,如下所示:
arry = String[]
append!(arry, "test")
但出现以下错误:
Cannot `convert` an object of type Char to an object of type String
为什么添加字符串而不是字符时出现此错误?
(下面为其他Julia新手的利益发布了我自己的答案-有更多有用提示的人,请跳进来!)
我看不到您的答案,但这是典型的模式。
您可以push!
单个元素或append!
集合:
julia> arry = String[]
0-element Array{String,1}
julia> push!(arry, "test")
1-element Array{String,1}:
"test"
julia> append!(arry, ("test",))
2-element Array{String,1}:
"test"
"test"
julia> append!(arry, ["test"])
3-element Array{String,1}:
"test"
"test"
"test"
julia> append!(arry, Ref("test"))
4-element Array{String,1}:
"test"
"test"
"test"
"test"
注意,集合可以是例如a Tuple
,aVector
或Ref
(这是在广播中经常遇到的0维且非分配的集合)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句