Push into array of arrays does not work when type is specified

Why the following code works

using Combinatorics

all_sequences = []
for i in 2:4
    for j in permutations(1:4, i)
        push!(all_sequences, collect(j))
    end
end

and this one does not

using Combinatorics

all_sequences = Array{Array{Int64,1},1}[]
for i in 2:4
    for j in permutations(1:4, i)
        push!(all_sequences, collect(j))
    end
end

and gives the error message: ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Array{Int64,1}.

When I specify the array all_sequences type as Array{Array{Int64,1},1}, I got the error. However, the array all_sequences type is nothing but Array{Array{Int64,1},1}, no?

Your second code creates an array-of-arrays-of-arrays:

> Array{Array{Int64,1},1}[] |> typeof
Vector{Vector{Vector{Int64}}}

To create array-of-arrays do:

> Vector{Int64}[] |> typeof
Vector{Vector{Int64}}

or

> Vector{Vector{Int64}}() |> typeof
Vector{Vector{Int64}}
5 Likes