Multidimensional Arrays in Swift

Change: As Adam Washington points out since beta 6, this code works as it is, so the question is no longer relevant.

I am trying to create and iterate over 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 seems that the last column in the row overwrites the values โ€‹โ€‹of the other columns.

Am I declaring this wrong?

Change: Perhaps a picture from the playground will help:

Captured from playground

+47
arrays swift
Jun 05 '14 at 4:08
source share
7 answers

For future readers, here is an elegant solution (5x5):

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

and dynamic approach:

 var matrix = [[Int]]() // creates an empty matrix var row = [Int]() // fill this row matrix.append(row) // add this row 
+9
Apr 15 '16 at 20:07
source share

As pointed out in other answers, you add 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())) } 
+53
Jun 05 '14 at 5:28
source share
 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 are looking for, you need to initialize the array to the correct pattern, and then loop to add arrays of rows and columns:

 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)") 
+14
Jun 05 '14 at 4:29
source share

Perhaps your problem is due to a lack of an earlier version of Swift or a beta version of Xcode. Working with Xcode 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 the Swift playground and defined two constants:

  let NumColumns = 3, NumRows = 3 
+5
Aug 21 '14 at 15:46
source share

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 } } 
+4
Sep 29 '14 at 2:51
source share

You create an array of three elements and assign all three of the same thing, which itself is an array of three elements (three pairs).

When you make changes, you change the floats in the internal array.

+1
Jun 05 '14 at 4:41
source share

Your original logic for creating the matrix is โ€‹โ€‹really correct, and it even works in Swift 2. The problem is that in the print cycle the variable rows and columns are reversed. 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!

+1
Oct 11 '15 at 16:12
source share



All Articles