You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Array2D/README.markdown
+14-16Lines changed: 14 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
# Array2D
2
2
3
-
In C and Objective-C you can write the following line,
3
+
In C and Objective-C, you can write the following line,
4
4
5
5
int cookies[9][7];
6
6
7
-
to make a 9x7 grid of cookies. This would create a two-dimensional array of 63 elements. To find the cookie at column 3, row 6, you'd write:
7
+
to make a 9x7 grid of cookies. This creates a two-dimensional array of 63 elements. To find the cookie at column 3 and row 6, you can write:
8
8
9
9
myCookie = cookies[3][6];
10
10
11
-
Unfortunately, you can't write the above in Swift. To create a multi-dimensional array in Swift you'd have to do something like this:
11
+
This statement is not acceptable in Swift. To create a multi-dimensional array in Swift, you can write:
12
12
13
13
```swift
14
14
var cookies = [[Int]]()
@@ -21,33 +21,33 @@ for _ in 1...9 {
21
21
}
22
22
```
23
23
24
-
And then to find a cookie:
24
+
Then, to find a cookie, you can write:
25
25
26
26
```swift
27
27
let myCookie = cookies[3][6]
28
28
```
29
29
30
-
Actually, you could create the array in a single line of code, like so:
30
+
You can also create the array in a single line of code:
31
31
32
32
```swift
33
33
var cookies = [[Int]](repeating: [Int](repeating: 0, count: 7), count: 9)
34
34
```
35
35
36
-
but that's just ugly. To be fair, you can hide the ugliness in a helper function:
36
+
This looks complicated, but can simplfy it in a helper function:
37
37
38
38
```swift
39
39
funcdim<T>(_count: Int, _value: T) -> [T] {
40
40
return [T](repeating: value, count: count)
41
41
}
42
42
```
43
43
44
-
And then creating the array looks like this:
44
+
Then, you can create the array:
45
45
46
46
```swift
47
47
var cookies =dim(9, dim(7, 0))
48
48
```
49
49
50
-
Swift infers that the datatype of the array should be `Int` because you specified `0` as the default value of the array elements. To use a string instead, you'd write:
50
+
Swift infers that the datatype of the array must be `Int` because you specified `0` as the default value of the array elements. To use a string instead, you can write:
51
51
52
52
```swift
53
53
var cookies =dim(9, dim(7, "yum"))
@@ -59,9 +59,9 @@ The `dim()` function makes it easy to go into even more dimensions:
59
59
var threeDimensions =dim(2, dim(3, dim(4, 0)))
60
60
```
61
61
62
-
The downside of using multi-dimensional arrays in this fashion -- actually, multiple nested arrays -- is that it's easy to lose track of what dimension represents what.
62
+
The downside of using multi-dimensional arrays or multiple nested arrays in this way is to lose track of what dimension represents what.
63
63
64
-
So instead let's create our own type that acts like a 2-D array and that is more convenient to use. Here it is, short and sweet:
64
+
Instead, you can create your own type that acts like a 2-D array which is more convenient to use:
65
65
66
66
```swift
67
67
publicstructArray2D<T> {
@@ -92,26 +92,24 @@ public struct Array2D<T> {
92
92
93
93
`Array2D` is a generic type, so it can hold any kind of object, not just numbers.
94
94
95
-
To create an instance of `Array2D` you'd write:
95
+
To create an instance of `Array2D`, you can write:
96
96
97
97
```swift
98
98
var cookies =Array2D(columns: 9, rows: 7, initialValue: 0)
99
99
```
100
100
101
-
Thanks to the `subscript` function, you can do the following to retrieve an object from the array:
101
+
By using the `subscript` function, you can retrieve an object from the array:
102
102
103
103
```swift
104
104
let myCookie = cookies[column, row]
105
105
```
106
106
107
-
Or change it:
107
+
Or, you can change it:
108
108
109
109
```swift
110
110
cookies[column, row] = newCookie
111
111
```
112
112
113
-
Internally, `Array2D` uses a single one-dimensional array to store the data. The index of an object in that array is given by `(row x numberOfColumns) + column`. But as a user of `Array2D` you don't have to worry about that; you only have to think in terms of "column" and "row", and let `Array2D` figure out the details for you. That's the advantage of wrapping primitive types into a wrapper class or struct.
114
-
115
-
And that's all there is to it.
113
+
Internally, `Array2D` uses a single one-dimensional array to store the data. The index of an object in that array is given by `(row x numberOfColumns) + column`, but as a user of `Array2D`, you only need to think in terms of "column" and "row", and the details will be done by `Array2D`. This is the advantage of wrapping primitive types into a wrapper class or struct.
116
114
117
115
*Written for Swift Algorithm Club by Matthijs Hollemans*
0 commit comments