Julia 中 String[] 和 Vector{String}[] 的区别
Difference between String[] and Vector{String}[] in Julia
我有以下代码:
x = String[]
y = Vector{String}[]
x == y # is true
push!(x, "a") # works fine
push!(y, "a") # ERROR
错误信息是:
ERROR: MethodError: Cannot `convert` an object of type String to an object of type Vector{String}
String[]
和 Vector{String}[]
有什么区别?它们不都是字符串向量吗?
T[]
其中 T
是一个类型,生成一个 zero-length 向量,其元素类型为 T
。所以你的 y
是 Vector{String}
的向量。因此,如果您尝试将字符串推送给它,您会得到一个错误。尝试推送一个字符串向量。
我有以下代码:
x = String[]
y = Vector{String}[]
x == y # is true
push!(x, "a") # works fine
push!(y, "a") # ERROR
错误信息是:
ERROR: MethodError: Cannot `convert` an object of type String to an object of type Vector{String}
String[]
和 Vector{String}[]
有什么区别?它们不都是字符串向量吗?
T[]
其中 T
是一个类型,生成一个 zero-length 向量,其元素类型为 T
。所以你的 y
是 Vector{String}
的向量。因此,如果您尝试将字符串推送给它,您会得到一个错误。尝试推送一个字符串向量。