mac01021
mac01021

Reputation: 825

Julia: Index an array with Int64?

The following code complains

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

Should I be able to do this? The problem, I think, is that the loop variable has the wrong type to be used as an 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))

Upvotes: 3

Views: 805

Answers (1)

waTeim
waTeim

Reputation: 9235

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

A = Array{Int32,n}

julia> typeof(A)
DataType

This declares an A to be the type representing an array of n dimensions. What you want instead, probably is A to be a variable of type Array{Int32,1} that contains n elements. So instead try the following:

A = Array(Int32,n);

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

Upvotes: 4

Related Questions