Lesson 10 - Multidimensional arrays in Swift
In the previous exercise, Solved tasks for Swift lesson 9, we've practiced our knowledge from previous lessons.
Today's Swift tutorial is basically a bonus when it comes to basic constructs. We'll discuss what we call multidimensional arrays.
We've already worked with a one-dimensional array which we can imagine as a row of boxes in computer memory.

(An array of eight numbers can be seen in the image)
Although it's not too common, you may sometimes encounter multidimensional arrays. Especially, when it comes to game applications.
Two-dimensional array
A good representation of a 2-dimensional array is a grid because technically, it is one. A practical application for 2-dimensional arrays would be to use them to store the available seats in a cinema. Here's a visual representation of what I'm referring to:

*(We can see the available seats of a cinema in the picture)
Of course, the cinema would be bigger in real life, but this array is just
fine as an example. 0
means the seat is available, 1
stands for one that isn't. Later, we could also add 2
for reserved
seats and so on. It would be more appropriate to create our own data type
(called enumerable) for these states, but we'll get into that later. For now,
we'll work with numbers.
In Swift, we initialize a 2D array like this:
var cinema = [[Int]]()
Actually, it's just an array of arrays. That means that each element of this array is an array too.
Above, we have successfully declared a multidimensional array but filling it with zeros to be able to represent a cinema would be ineffective. That's why we'll create the array other way:
var cinema = Array(repeating: Array(repeating: 0, count: 5), count: 5)
The Array
class makes it easy to create arrays with recurring
values. Of course, we could also use it for traditional 1D arrays. The
repeating
parameter specifies what will be repeated and
count
the number of times. So, for the first array, we'll generate
another inner array five times. We'll generate zeros for the inner array.
We can print our 2D array to check it out. The first loop will go through each array and the inner loop will then print their values:
for array in cinema { for value in array { print(value, terminator: " ") } print(" ") }
The output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Filling with data
Let's fill the cinema room with 1s now as you can see in the picture above.
Since we'll be lazy as good programmers should be, we'll use for
loops to create a row of 1s To
access an item of a 2D array we have to enter two coordinates. If the array
wasn't filled with zeros (or other values) first, this code wouldn't work.
cinema[2][2] = 1 // Center for i in 1..<4 { // 4th row cinema[3][i] = 1 } for i in 0..<5 { // Last row cinema[4][i] = 1 }
We can print the array again to test it:
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 1 1 1
N-dimensional arrays
Sometimes, it may be useful to create an array of even more dimensions. We can all at least imagine a 3D array. Adding on the cinema analogy, we'll say ours has multiple floors, or generally more rooms. The visualization could then look like this:

We can create a 3D array the same way we created the 2D array:
var cinemas = [[[Int]]]()
Alternatively, we can use the Array
class:
var cinemas = Array(repeating: Array(repeating: Array(repeating: 0, count: 5), count: 5), count: 3)
The principle is still the same, it's just harder to comprehend
The code above creates the 3D array you saw in the picture. We can access it through the indexers, square brackets, as before, but now we have to enter 3 coordinates.
cinemaRooms[1][2][3] = 1 // the second-floor cinema, the third row, the fourth seat
We could print our cinema, for example, like this:
var floor = 1 for cinema in cinemas { print("Floor: \(floor)") floor += 1 for array in cinema { for value in array { print(value, terminator: " ") } print(" ") } print("-----------------") }
The output after assigning the number 1 in the code above:
Floor: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ----------------- Floor: 2 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 ----------------- Floor: 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -----------------
In conclusion, I would like to add that some people who can't use objects
properly, use 2D arrays to store multiple sets of data of a single entity. e.g.
imagine that we want to store the length, width, and height of five cell phones.
Although you may think that a 3D array would be best for the situation, it can
be pulled off with an ordinary 1D array (specifically a list of objects of the
Phone
type). We'll go over all of that in the object-oriented
programming course. You should also definitely give the exercises for this
lesson a shot.
In the next lesson, Mathematical functions in Swift, we'll look at basic math functions and finish the basic constructs course.
Download
By downloading the following file, you agree to the license termsDownloaded 719x (33.84 kB)