✅ 2D Array in Swift
// Define a 2D array (3x3 matrix) var matrix: [[Int]] = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] // Access element at row 1, column 2 (value = 6) print("Element at [1][2] is:", matrix[1][2]) // Modify element at [2][1] (change 8 to 88) matrix[2][1] = 88 // Iterate through the 2D array for row in matrix { for value in row { print(value, terminator: " ") } print() }
🧾 Output:
Element at [1][2] is: 6
1 2 3
4 5 6
7 88 9
Top comments (0)