Difference between revisions of "W2021 Two-Dimensional Arrays"
(Created page with "Two-dimensional arrays are Swift arrays whose elements are other arrays. However, this is not the only interesting property of two-dimensional arrays. Two-dimensional arrays i...") |
|||
Line 1: | Line 1: | ||
As previously mentioned 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 have 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: | |||
<syntaxhighlight> | |||
[["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"]] | |||
</syntaxhighlight> | |||
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 == | |||
=== Without A Predefined Size === | |||
To create a two-dimensional array without a predefined size, a similar construct to a single-dimensional array may be used: | |||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
var twoDim = [[String]]() | |||
var | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Note the double brackets '''[ [''' and '''] ]''' which define this as a two-dimensional array. The first level of the array is an array that can hold an array that holds strings. Data can be written to such an array using '''append''' as usual: | |||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
twoDim.append(["This", "is", "an", "array"]) | |||
twoDim.append(["This", "is", "another", "array"]) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
=== With A Predefined Size === | |||
In order to create an array with a predefined size, you can use the '''Array''' constructor: | |||
== | |||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
var twoDim = Array(repeating: Array(repeating: "", count: 10), count: 10) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
This array | This will create an array with 10 elements, each of which is an array of 10 strings. | ||
== | === With A Predefined Array === | ||
If the two-dimensional array is already known (or at least a default), then it can be assigned to a variable just like a one-dimensional array: | |||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
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"]] | |||
</syntaxhighlight> | </syntaxhighlight> | ||
The | |||
== Indexes == | |||
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. In order to access a particular 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 will be 2 and then 4 (because array indexing starts at zero): | |||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
twoDim[2][4] // R3C5 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
{{CodeExplorer | {{CodeExplorer | ||
|exerciseID= | |exerciseID=1 | ||
|height=250 | |||
|mode=swift | |mode=swift | ||
|initialCode= | |||
|initialCode= | var twoDim = [["R1C1", "R1C2", "R1C3", "R1C4", "R1C5", "R1C6", "R1C7", "R1C8", "R1C9", "R1C10"], | ||
["R2C1", "R2C2", "R2C3", "R2C4", "R2C5", "R2C6", "R2C7", "R2C8", "R2C9", "R2C10"], | |||
var | ["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"]] | |||
print(twoDim[2][4]) | |||
}} | }} | ||
== Loops == | |||
Similar to one-dimensional arrays, two-dimensional arrays are also commonly used within for loops. For example, to print each inner array on its own line, a single for loop can be used: | |||
== | |||
{{CodeExplorer | {{CodeExplorer | ||
|exerciseID= | |exerciseID=2 | ||
|height=250 | |||
|mode=swift | |mode=swift | ||
| | |initialCode= | ||
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"]] | |||
func prettyPrint(twoDim: [[String]]) { | |||
for row in 0..<twoDim.count { | |||
print(twoDim[row]) | |||
} | |||
} | |||
prettyPrint(twoDim: twoDim) | |||
}} | }} | ||
However, since each element of the outer array is itself an array, two levels of for loops are required to interact with each indivudal element. For example, in order to initialize '''twoDim''' with each row and column labelled, two for loops can be used to keep track of the row and then column: | |||
{{CodeExplorer | {{CodeExplorer | ||
|exerciseID= | |exerciseID=3 | ||
|height=250 | |||
|height= | |||
|mode=swift | |mode=swift | ||
|initialCode= | |||
|initialCode= | |||
// Create a two-dimensional array with 9 rows and 10 columns | |||
var twoDim = Array(repeating: Array(repeating: "", count: 10), count: 9) | |||
// Iterate through each row | |||
for row in 0..<twoDim.count { | |||
// Iterate through each column | |||
for col in 0..<twoDim[row].count { | |||
twoDim[row][col] = "R\(row + 1)C\(row+1)" | |||
} | |||
} | |||
// | // Print each row on its own line | ||
func prettyPrint(twoDim: [[String]]) { | |||
for row in 0..<twoDim.count { | |||
print(twoDim[row]) | |||
} | |||
} | |||
prettyPrint(twoDim: twoDim) | |||
}} | }} | ||
Revision as of 23:06, 31 October 2021
As previously mentioned 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 have 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, a similar construct to a single-dimensional array may be used:
var twoDim = [[String]]()
Note the double brackets [ [ and ] ] which define this as a two-dimensional array. The first level of the array is an array that can hold an array that holds 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]
In order 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 will create 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), then 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. In order to access a particular 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 will be 2 and then 4 (because array indexing starts at zero):
twoDim[2][4] // R3C5
Loops[edit]
Similar to one-dimensional arrays, two-dimensional arrays are also commonly used within for loops. For example, to print each inner array on its own line, a single for loop can be used:
However, since each element of the outer array is itself an array, two levels of for loops are required to interact with each indivudal element. For example, in order to initialize twoDim with each row and column labelled, two for loops can be used to keep track of the row and then column: