Multidimensional arrays in Swift

后端 未结 7 962
离开以前
离开以前 2020-11-30 22:43

Edit: As Adam Washington points out, as from Beta 6, this code works as is, so the question is no longer relevant.

I am trying to create and iterate through a two d

相关标签:
7条回答
  • 2020-11-30 22:48

    For future readers, here is an elegant solution(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
    
    0 讨论(0)
  • 2020-11-30 22:48

    Using http://blog.trolieb.com/trouble-multidimensional-arrays-swift/ as a start, I added generics to mine:

    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
    }
    }
    
    0 讨论(0)
  • 2020-11-30 22:52

    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()))
    }
    
    0 讨论(0)
  • 2020-11-30 23:01
    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)")
    
    0 讨论(0)
  • 2020-11-30 23:04

    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.

    0 讨论(0)
  • 2020-11-30 23:12

    Your problem may have been due to a deficiency in an earlier version of Swift or of the Xcode Beta. Working with Xcode Version 6.0 (6A279r) on August 21, 2014, your code works as expected with this output:

    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:

    let NumColumns = 3, NumRows = 3
    0 讨论(0)
提交回复
热议问题