W2021 Two-Dimensional Arrays

From Coder Merlin
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Introduction[edit]

As covered earlier in W1301_Arrays, an array is a variable that holds many elements of the same/homogeneous type. A two-dimensional array follows the same principles, with the elements of the array being more arrays. Each of those arrays, in turn, has elements of the same type.

Two-dimensional arrays are especially useful when representing data that is in rows and columns, or a conceptually similar format such as matrices. For example, here is a two-dimensional array with 9 rows and 10 columns:

[["R1C1", "R1C2", "R1C3", "R1C4", "R1C5", "R1C6", "R1C7", "R1C8", "R1C9", "R1C10"],
["R2C1", "R2C2", "R2C3", "R2C4", "R2C5", "R2C6", "R2C7", "R2C8", "R2C9", "R2C10"],
["R3C1", "R3C2", "R3C3", "R3C4", "R3C5", "R3C6", "R3C7", "R3C8", "R3C9", "R3C10"],
["R4C1", "R4C2", "R4C3", "R4C4", "R4C5", "R4C6", "R4C7", "R4C8", "R4C9", "R4C10"],
["R5C1", "R5C2", "R5C3", "R5C4", "R5C5", "R5C6", "R5C7", "R5C8", "R5C9", "R5C10"],
["R6C1", "R6C2", "R6C3", "R6C4", "R6C5", "R6C6", "R6C7", "R6C8", "R6C9", "R6C10"],
["R7C1", "R7C2", "R7C3", "R7C4", "R7C5", "R7C6", "R7C7", "R7C8", "R7C9", "R7C10"],
["R8C1", "R8C2", "R8C3", "R8C4", "R8C5", "R8C6", "R8C7", "R8C8", "R8C9", "R8C10"],
["R9C1", "R9C2", "R9C3", "R9C4", "R9C5", "R9C6", "R9C7", "R9C8", "R9C9", "R9C10"]]

It is worth noting that the indexes for the array still start at zero; however, there is generally no row 0 or column 0, so the elements have been incremented to represent this.

Defining a Two-Dimensional Array in Swift[edit]

Without a Predefined Size[edit]

To create a two-dimensional array without a predefined size, use a similar construct to a single-dimensional array:

var twoDim = [[String]]()

Note that the double brackets [ [ and ] ] define this as a two-dimensional array. The first level of the array can hold more arrays that, in turn, hold strings. Data can be written to such an array using append as usual:

twoDim.append(["This", "is", "an", "array"])
twoDim.append(["This", "is", "another", "array"])

With a Predefined Size[edit]

To create an array with a predefined size, you can use the Array constructor:

var twoDim = Array(repeating: Array(repeating: "", count: 10), count: 10)

This creates an array with 10 elements, each of which is an array of 10 strings.

With a Predefined Array[edit]

If the two-dimensional array is already known (or at least a default), it can be assigned to a variable just like a one-dimensional array:

var twoDim = [["R1C1", "R1C2", "R1C3", "R1C4", "R1C5", "R1C6", "R1C7", "R1C8", "R1C9", "R1C10"],
              ["R2C1", "R2C2", "R2C3", "R2C4", "R2C5", "R2C6", "R2C7", "R2C8", "R2C9", "R2C10"],
              ["R3C1", "R3C2", "R3C3", "R3C4", "R3C5", "R3C6", "R3C7", "R3C8", "R3C9", "R3C10"],
              ["R4C1", "R4C2", "R4C3", "R4C4", "R4C5", "R4C6", "R4C7", "R4C8", "R4C9", "R4C10"],
              ["R5C1", "R5C2", "R5C3", "R5C4", "R5C5", "R5C6", "R5C7", "R5C8", "R5C9", "R5C10"],
              ["R6C1", "R6C2", "R6C3", "R6C4", "R6C5", "R6C6", "R6C7", "R6C8", "R6C9", "R6C10"],
              ["R7C1", "R7C2", "R7C3", "R7C4", "R7C5", "R7C6", "R7C7", "R7C8", "R7C9", "R7C10"],
              ["R8C1", "R8C2", "R8C3", "R8C4", "R8C5", "R8C6", "R8C7", "R8C8", "R8C9", "R8C10"],
              ["R9C1", "R9C2", "R9C3", "R9C4", "R9C5", "R9C6", "R9C7", "R9C8", "R9C9", "R9C10"]]


Indexes[edit]

Similar to a one-dimensional array, every element in a two-dimensional array has a unique index. In addition, the syntax for retrieving and setting an element is the same. The only difference is that after the first index is passed, an array is returned. To access an element, two levels of indexes need to be used; one for the "outer" array and one for the "inner" array. For example, to retrieve the element at row 3 column 5, the indexes must be 2 and 4 (because array indexing starts at zero):

twoDim[2][4] // R3C5

CoderMerlin™ Code Explorer: W0000 (1) 🟢


Loops[edit]

Similar to one-dimensional arrays, two-dimensional arrays are also commonly used in for loops. For example, to print each inner array on its own line, a single for loop can be used:

CoderMerlin™ Code Explorer: W0000 (2) 🟢


However, since each element of the outer array is itself an array, two levels of for loops are required to interact with each individual element. For example, to initialize twoDim with each row and column labeled, two for loops can be used to keep track of the row and then column:


CoderMerlin™ Code Explorer: W0000 (3) 🟢