更新:糟糕,正如下面(非常客氣地)@ rickhg12hs指出的,我只是閱讀錯誤的手冊。 insert!
完全有效。無法插入! (但推!作品)
在Julia中,push!
適用於許多不同類型的列表,但insert!
不適用。
例如,考慮類型ASCIIString的數組:
x = ["a", "b"]
push!(x, "c")
結果x = ["a", "b", "c"]
。但是:
x = ["a", "b"]
insert!(x, "c", 3)
結果:
ERROR: `insert!` has no method matching insert!(::Array{ASCIIString,1}, ::ASCIIString, ::Int64)
鑑於此,怎麼能插入ASCIIString
成一個Array{ASCIIString, 1}
的任意位置?
簡單地重新分配整個陣列是最好的選擇嗎?例如:
x = [x[1:insertIndex-1], itemToInsert, x[insertIndex:end]]
'Base.insert!(collection,index,item)'是簽名。 – jverzani 2014-09-05 11:53:24