插入到整数数组中迅速 [英] Inserting integer into array in swift

查看:81
本文介绍了插入到整数数组中迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不与斯威夫特还点有是开始被恼人的一点点问题。

I'm not really on point with Swift yet and there is a problem that is starting to be a tad annoying.

我只是想在双维阵列添加整数,但它总是返回相同的错误code:致命错误:数组索引超出范围

I just want to add integer in a double dimensional array but it is always returning the same error code : "fatal error : Array index out of range"

var arrayVolley = [[Int]]()

init(){
    self.arrayVolley = [[]]
}

下面是我尝试插入:

func addPoints(score : Int, x : Int, y : Int){

    if (score > 11 || score < 0){ //11 will be translated as 10x
        println("Error on score value")
    }
    else {
        if (x>6 || y>6){
            println("Out of array")
        }
        else{
            arrayVolley[x][y]=score
        }
    }
}

这是我的主要:

var i=0
var j=0
for i in 0...6 {
    for j in 0...6{
        println("Entrez le score")
        var scoreinput=input()
        var score = scoreinput.toInt()
        distance.addPoints(score!, x: i, y: j)
    }
}

在此先感谢了很多的帮助。

Thanks a lot for your help in advance

推荐答案

尝试使用追加到整数添加到阵列自动为下一IDEX。它认为,如果指数从未使用过它例如给出了一个错误

Try to use append to add the integer to the array it is automatically the next idex. It think if the index was never used it gives an error e.g.

var test = [Int]()
test.append(2) // array is empty so 0 is added as index
test.append(4)
test.append(5) // 2 is added as max index array is not [2,4,5]
test[0] = 3 // works because the index 0 exist cause the where more then 1 element in array -> [3,4,5]
test[4] = 5 // does not work cause index for never added with append 

或你intialize正确的大小数组,但它需要一个尺寸:

or you intialize the array in the correct size, but it's need a size:

var test = [Int](count: 5, repeatedValue: 0) // [0,0,0,0,0]
test[0] = 3 //[3,0,0,0,0]
test[4] = 5 [3,0,0,0,5]

它希望这可以帮助你,如果不是请随时发表评论。

It hope this helps you if not please feel free to comment.

这篇关于插入到整数数组中迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆