gpt4 book ai didi

Julia:使用 Int64 索引数组?

转载 作者:行者123 更新时间:2023-12-01 12:38:41 24 4
gpt4 key购买 nike

下面的代码提示

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

我应该能够做到这一点吗?我认为,问题在于循环变量的类型错误,无法用作数组索引?

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))

最佳答案

你对 A 的赋值是合法的,但它并没有按照你的想法去做。

A = Array{Int32,n}

julia> typeof(A)
DataType

这声明一个 A 是表示 n 维数组的类型。相反,您想要的可能是 A 成为包含 n 元素的 Array{Int32,1} 类型的变量。因此,请尝试以下操作:

A = Array(Int32,n);

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

关于Julia:使用 Int64 索引数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27029110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com