Examples of adding elements and rows:
Using the previous vCR, which equals [[1,13],[2],[56,57,58]]…
vCR += [[2],[3,4]]
Remembering that vCR is a 2-dimensional array, we can only add 2-dimensional arrays to it (‘add like to like’). Here we have added 2 rows, the first row containing one column (2), and the second row containing two columns (3 and 4).
vCR now equals [[1,13],[2],[56,57,58],[2],[3,4]].
vCR[2] += [59,60]
vCR[2] represents the 3rd row, a single-dimensional array equal to [56,57,58]. We have added another two elements, 59 and 60. vCR[2] now equals [56,57,58,59,60], and vCR equals [[1,13],[2],[56,57,58,59,60],[2],[3,4]].
board.append([Int](count:3, repeatedValue:10))
‘board‘, from a previous paragraph, is an empty 2-dimensional array. We have appended 1 row of 3 columns. Each column contains the value 10. board now equals [[10,10,10]].
board[0].append(11)
board[0] is the first row of board. That is, a one-dimensional array. Hence we can only add elements to it, and in this case, we have added one more element, 11. board is now equal to [[10,10,10,11]].
vCR = [[1]] + [[1,2],[3,4,5,6]]
vCR now equals [[1],[1,2],[3,4,5,6]].
Some more examples:
vCR[2][3] will return the element in the third row, and the fourth column, i.e. 6.
vCR[2][3] += 2
vCR[2][3] will now equal 8. Note, vCR[2][3] is an integer, so we can only add an integer.
vCR[2] += [9]
vCR[2] will now equal [3,4,5,8,9]. Note vCR[2] is a one-dimensional array, so we can only add another one-dimensional array
vCR += [[100,200]]
vCR now equals [[1],[1,2],[3,4,5,8,9],[100,200]]. Note, vCR is a 2-dimensional array, so we can only add another 2-dimensional array.
Methods:
board.count board[0].count board.isEmpty board[0].isEmpty board.append([Int]()) board[1].isEmpty
Remember that board equals [[10,10,10,11]]. Therefore, board.count returns the number of rows in board, that is, 1. board[0].count returns the number of elements in the first row of board, that is, 4. board.isEmpty and board[0].isEmpty both return false, as board has rows, and board[0] has 4 elements.
After appending an empty row to board, board[1].isEmpty (the second and newest row) returns true.
Accessing elements:
vCR equals [[1],[1,2],[3,4,5,8,9],[100,200]].
println(vCR[2][3]) // prints 8 println(vCR[2]) // prints [3,4,5,8,9] println(vCR[2].first!) // prints 3 println(vCR[2].last!) // prints 9 println(vCR[2][0...2]) // prints [3,4,5] println(vCR[2][0..<2]) // prints [3,4]