错误:“数组索引超出范围”在多维数组中 [英] Error: "array index out of range" in multidimensional array
本文介绍了错误:“数组索引超出范围”在多维数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我声明了我的数组
var tile = [[Int]]()
之后我将其值初始化为
for (var index = 0; index < 4; index++)
{
for (var sindex = 0; sindex < 4; sindex++)
{
self.tile[index][sindex] = 0 // error here
println("\(index) \(sindex)")
}
}
在运行时它给出错误数组索引超出范围
At run time it is giving an error "array index out of range"
推荐答案
评论员@C_X& @MartinR说,你的数组是空的。以下是如何根据需要初始化它...
As the commentators @C_X & @MartinR say, your array is empty. Here's how to initialise it as you want...
var tile = [[Int]](count:4, repeatedValue: [Int](count: 4, repeatedValue: 0))
for index in 0 ..< 4 {
for sindex in 0 ..< 4 {
tile[index][sindex] = 0 // no error here now...
print("\(index) \(sindex)")
}
}
...当然,
循环现在是多余的,如果你只想要零!
...of course, the for
loops are now redundant, if you just want zeroes!
这篇关于错误:“数组索引超出范围”在多维数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文