i would like to populate an array of arrays with integers in julia. the following works:
a = Array{Int64}[]
push!(a, [1,2,3])
but this doesn't:
a = Array{Array{Int64}}[]
push!(a, [1, 2, 3])
the error is: MethodError: Cannot `convert` an object of type Int64 to an object of type Array{Int64,N} where N
can someone explain why? it seems like Array{Array{Int64}} should be the type of array whose elements are arrays containing Int64 values whereas Array{Int64} is an array of integers. yet a = Array{Int64}[] seems to initialize an array of arrays containing integers and not an array of integers? can someone clarify the logic here?
T[]creates an emptyVector{T}. SoArray{Int}[]creates aVector{Array{T}}. Be aware that you should not useArray{T}. Always useVector{T}instead, or alternatively specify the number of dimensions, as inArray{Int, 1}. My advice: always useVector{T}andMatrix{T}instead ofArray{T, 1}andArray{T, 2}. Never useArray{T}, it creates an abstract type.Julia provides the Vector and Matrix constructor functions, but these are simply aliases for uninitialized one and two dimensional arrays- isn'tVectoran alias forArray? The default type for[1,2,3]is array.Vector{T}is an alias forArray{T, 1}andMatrix{T}is an alias forArray{T,2}. The problem is that people always forget to specify the dimensionality, writingArray{T}instead. This is an abstract type, because it's a union of all arrays of all dimensionalities with the element typeT. WritingVectororMatrixensures that you do not forget the dimensionality, and it makes your code more readable.