-3

I have this defined variable which is supposed to be a 2d array of ints. How can I make it so that the 2D array is 4x4 and the values are all 0?

var table: [[Int]] 

jim1863
  • 1
  • 1

1 Answers1

0

You can use the Array.init(repeating:count:) initializer.

Example:

let row = Array(repeating: 0, count: 4) // [0, 0, 0, 0]

var table = Array(repeating: row, count: 4)

Result:

print(table)
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
George
  • 25,988
  • 10
  • 79
  • 133