Julia: specify an array with Int64?

The following code complains

ERROR: `setindex!` has no method matching setindex!(::Type{Array{Int32,32}}, ::Int32, ::Int64)

      

Should I do this? The problem I think is that the loop variable is of the wrong type to be used as the array index?

n = parseint(readline(STDIN))
A = Array{Int32, n}
for i in 1:n-1
    ai = parseint(Int32, readuntil(STDIN, ' '))
    A[i] = ai #The error happens here!
end
A[n] = parseint(Int32, readline(STDIN))

      

+3


source to share


1 answer


Your assignment A is legal, but it doesn't do what you think it does.

A = Array{Int32,n}

julia> typeof(A)
DataType

      



Declares A type representing an array of n dimensions. Instead, you want to use A as an Array {Int32,1} variable containing n elements . So try this:

A = Array(Int32,n);

julia> typeof(A)
Array{Int32,1}

      

+4


source







All Articles