我正在尝试运行 2 for 循环来访问数组中的 2 个元素,(例如)
x = 100
for i in eachindex(x-1)
for j in 2:x
doSomething = Array[i] + Array[j]
end
end
并且经常(并非总是)我收到此错误或类似错误:
LoadError: BoundsError: attempt to access 36-element Array{Any,1} at index [64]
我知道有适当的方法来运行这些循环以避免边界错误,因此我使用eachindex(x-1) == 1:x
, 我将如何做到这一点2:x
?
我对 Julia 比较陌生,如果这不是边界错误的原因,那可能是什么?- 谢谢
编辑:我正在尝试运行的缩短版本(也是,矢量数组)
all_people = Vector{type_person}() # 1D Vector of type person
size = length(all_people)
... fill vector array to create an initial population of people ...
# Now add to the array using existing parent objects
for i in 1:size-1
for j in 2:size
if all_people[i].age >= all_people[j].age # oldest parent object forms child variable
child = type_person(all_people[i])
else
child = type_person(all_people[j])
end
push!(all_people, child) # add to the group of people
end
end