在夫特二维阵列 [英] Two-dimensional array in Swift

查看:91
本文介绍了在夫特二维阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这么困惑斯威夫特二维数组。让我描述一步一步来。而且请你纠正我,如果我错了。

首先,空数组的声明:

 类的测试{
    VAR my2Darr = INT [] []()
}

其次填充阵列。 (如 my2Darr [I] [J] = 0 其中i,j是for循环变量)

 类的测试{
    VAR my2Darr = INT [] []()
    在里面() {
        对于(VAR我:= 0;我小于10;我++){
            对于(VAR记者:INT = 0; J< 10; J ++){
                my2Darr [I] [J] = 18 / *这是正确的? * /
            }
        }
    }
}

和最后的数组元素编辑

 类的测试{
    VAR my2Darr = INT [] []()
    在里面() {
        .... //一样了code
    }
    FUNC编辑(编号:诠释,指数为:int){
        my2Darr [指数] [指数] =号
        // 它是否正确?如果指数是什么大
        //比我或j ......我们能控制,像
        如果(my2Darr [I] [J] ==无){...} * /
    }
}

这可能是noob问题,但目标C后,我变得很困惑。


解决方案

定义可变数组

  // 2整型数组的维数组
VAR ARR = [[INT]]()

  // 2整型数组的维数组
VAR ARR:[INT]] = []

或者如果你需要predefined大小的数组(如在评论提到@ 0x7FFFFFFF的):

  // 2设置为0数组大小的int数组的维数组是10x5
VAR ARR =阵列(数:3,repeatedValue:阵列(数:2,repeatedValue:0))

的位置变化元素

 改编[0] [1] = 18

 让myVar的= 18
改编[0] [1] = myVar的

更改子阵列

 改编[1] = [123,456,789]

 改编[0] + = 234

 改编[0] + = [345,678]

如果你有3x2的阵列或0(零),现在你有:

  [
  [0,0,234,345,678],// 5元素!
  [123,456,789]
  [0,0]
]

所以,要知道,子数组是可变的,你可以重新定义重新presented矩阵初始数组。

访问前检查大小/边界

 让= 0
令b = 1如果arr.count>一个与放大器;&放大器; ARR [A] .Count之间的> b {
    的println(ARR [A] [B])
}

说明:
相同的标记规则3和N维数组。

I get so confused about 2D arrays in Swift. Let me describe step by step. And would you please correct me if i am wrong.

First of all; declaration of an empty array:

class test{
    var my2Darr = Int[][]()
}

Secondly fill the array. (such as my2Darr[i][j] = 0 where i, j are for-loop variables)

class test {
    var my2Darr = Int[][]()
    init() {
        for(var i:Int=0;i<10;i++) {
            for(var j:Int=0;j<10;j++) {
                my2Darr[i][j]=18   /*  Is this correct?  */
            }
        }
    }
}

And Lastly, Editing element of in array

class test {
    var my2Darr = Int[][]()
    init() {
        ....  //same as up code
    }
    func edit(number:Int,index:Int){
        my2Darr[index][index] = number
        // Is this correct? and What if index is bigger
        // than i or j... Can we control that like 
        if (my2Darr[i][j] == nil) { ...  }   */
    }
}

This might be noob question but after objective C, I get really confused..

解决方案

Define mutable array

// 2 dimensional array of arrays of Ints 
var arr = [[Int]]() 

OR:

// 2 dimensional array of arrays of Ints 
var arr: [[Int]] = [] 

OR if you need an array of predefined size (as mentioned by @0x7fffffff in comments):

// 2 dimensional array of arrays of Ints set to 0. Arrays size is 10x5
var arr = Array(count: 3, repeatedValue: Array(count: 2, repeatedValue: 0))

Change element at position

arr[0][1] = 18

OR

let myVar = 18
arr[0][1] = myVar

Change sub array

arr[1] = [123, 456, 789] 

OR

arr[0] += 234

OR

arr[0] += [345, 678]

If you had 3x2 array or 0(zeros), now you have:

[
  [0, 0, 234, 345, 678], // 5 elements!
  [123, 456, 789],
  [0, 0]
]

So be aware that sub arrays are mutable and you can redefine initial array that represented matrix.

Examine size/bounds before access

let a = 0
let b = 1

if arr.count > a && arr[a].count > b {
    println(arr[a][b])
}

Remarks: Same markup rules for 3 and N dimensional arrays.

这篇关于在夫特二维阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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