简体   繁体   English

Swift中的多维数组

[英]Multidimensional arrays in Swift

Edit: As Adam Washington points out, as from Beta 6, this code works as is, so the question is no longer relevant. 编辑:正如亚当华盛顿指出的那样,从Beta 6开始,这段代码按原样运行,所以这个问题已经不再适用了。

I am trying to create and iterate through a two dimensional array: 我正在尝试创建并迭代二维数组:

var array = Array(count:NumColumns, repeatedValue:Array(count:NumRows, repeatedValue:Double()))

array[0][0] = 1
array[1][0] = 2
array[2][0] = 3
array[0][1] = 4
array[1][1] = 5
array[2][1] = 6
array[0][2] = 7
array[1][2] = 8
array[2][2] = 9

for column in 0...2 {
    for row in 0...2 {
        println("column: \(column) row: \(row) value:\(array[column][row])")
    }
}

However, this is the output I get: 但是,这是我得到的输出:

column: 0 row: 0 value:3.0
column: 0 row: 1 value:6.0
column: 0 row: 2 value:9.0
column: 1 row: 0 value:3.0
column: 1 row: 1 value:6.0
column: 1 row: 2 value:9.0
column: 2 row: 0 value:3.0
column: 2 row: 1 value:6.0
column: 2 row: 2 value:9.0

It looks as if the last column in the row is overwriting the other column values. 看起来该行中的最后一列正在覆盖其他列值。

Am I declaring it wrong? 我宣布错了吗?

Edit: Perhaps a picture from the Playground would help: 编辑:也许游乐场的照片会有所帮助:

从游乐场捕获

As stated by the other answers, you are adding the same array of rows to each column. 正如其他答案所述,您要为每列添加相同的行数组。 To create a multidimensional array you must use a loop 要创建多维数组,必须使用循环

var NumColumns = 27
var NumRows = 52
var array = Array<Array<Double>>()
for column in 0..NumColumns {
    array.append(Array(count:NumRows, repeatedValue:Double()))
}
var array: Int[][] = [[1,2,3],[4,5,6],[7,8,9]]

for first in array {
    for second in first {
        println("value \(second)")
    }
}

To achieve what you're looking for you need to initialize the array to the correct template and then loop to add the row and column arrays: 要实现您正在寻找的内容,您需要将数组初始化为正确的模板,然后循环以添加行和列数组:

var NumColumns = 27
var NumRows = 52
var array = Array<Array<Int>>()
var value = 1

for column in 0..NumColumns {
    var columnArray = Array<Int>()
    for row in 0..NumRows {
        columnArray.append(value++)
    }
    array.append(columnArray)
}

println("array \(array)")

For future readers, here is an elegant solution(5x5): 对于未来的读者,这是一个优雅的解决方案(5x5):

var matrix = [[Int]](repeating: [Int](repeating: 0, count: 5), count: 5)

and a dynamic approach: 和动态的方法:

var matrix = [[Int]]() // creates an empty matrix
var row = [Int]() // fill this row
matrix.append(row) // add this row

Your problem may have been due to a deficiency in an earlier version of Swift or of the Xcode Beta. 您的问题可能是由于早期版本的Swift或Xcode Beta的缺陷造成的。 Working with Xcode Version 6.0 (6A279r) on August 21, 2014, your code works as expected with this output: 在2014年8月21日使用Xcode版本6.0(6A279r),您的代码按此预期使用此输出:

column: 0 row: 0 value:1.0
column: 0 row: 1 value:4.0
column: 0 row: 2 value:7.0
column: 1 row: 0 value:2.0
column: 1 row: 1 value:5.0
column: 1 row: 2 value:8.0
column: 2 row: 0 value:3.0
column: 2 row: 1 value:6.0
column: 2 row: 2 value:9.0

I just copied and pasted your code into a Swift playground and defined two constants: 我只是将您的代码复制并粘贴到Swift游乐场并定义了两个常量:

let NumColumns = 3, NumRows = 3

Using http://blog.trolieb.com/trouble-multidimensional-arrays-swift/ as a start, I added generics to mine: 使用http://blog.trolieb.com/trouble-multidimensional-arrays-swift/作为开始,我将generics添加到我的:

class Array2DTyped<T>{

var cols:Int, rows:Int
var matrix:[T]

init(cols:Int, rows:Int, defaultValue:T){
    self.cols = cols
    self.rows = rows
    matrix = Array(count:cols*rows,repeatedValue:defaultValue)
}

subscript(col:Int, row:Int) -> T {
    get{
        return matrix[cols * row + col]
    }
    set{
        matrix[cols * row + col] = newValue
    }
}

func colCount() -> Int {
    return self.cols
}

func rowCount() -> Int {
    return self.rows
}
}

You are creating an array of three elements and assigning all three to the same thing, which is itself an array of three elements (three Doubles). 你正在创建一个由三个元素组成的数组,并将所有三个元素分配给同一个东西,它本身就是一个由三个元素组成的数组(三个双打)。

When you do the modifications you are modifying the floats in the internal array. 进行修改时,您将修改内部数组中的浮动。

Your original logic for creating the matrix is indeed correct, and it even works in Swift 2. The problem is that in the print loop, you have the row and column variables reversed. 您创建矩阵的原始逻辑确实是正确的,它甚至可以在Swift 2中运行。问题是在打印循环中,您可以反转行和列变量。 If you change it to: 如果您将其更改为:

for row in 0...2 {
  for column in 0...2 {
    print("column: \(column) row: \(row) value:\(array[column][row])")

  }
}

you will get the correct results. 你会得到正确的结果。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM