Lesson 10 - Multidimensional arrays in Kotlin
In the previous exercise, Solved tasks for Kotlin lesson 9, we've practiced our knowledge from previous lessons.
Lesson highlights
Are you looking for a quick reference on multidimensional arrays in Kotlin instead of a thorough-full lesson? Here it is:
Shortened initialization of a 2D array:
val cinema = arrayOf( arrayOf(0, 0, 0, 0, 1), arrayOf(0, 0, 0, 1, 1), arrayOf(0, 0, 1, 1, 1), arrayOf(0, 0, 0, 1, 1), arrayOf(0, 0, 0, 0, 1) )
Writing 1
at the position
[1][0]
:
cinema[1][0] = 1
Reading the value (now 1
) at the position
[1][0]
:
{KOTLIN_CONSOLE}
val cinema = arrayOf(
arrayOf(0, 0, 0, 0, 1),
arrayOf(0, 0, 0, 1, 1),
arrayOf(0, 0, 1, 1, 1),
arrayOf(0, 0, 0, 1, 1),
arrayOf(0, 0, 0, 0, 1)
)
cinema[1][0] = 1
println(cinema[1][0])
{/KOTLIN_CONSOLE}
Printing the whole 2D array:
{KOTLIN_CONSOLE}
val cinema = arrayOf(
arrayOf(0, 0, 0, 0, 1),
arrayOf(0, 0, 0, 1, 1),
arrayOf(0, 0, 1, 1, 1),
arrayOf(0, 0, 0, 1, 1),
arrayOf(0, 0, 0, 0, 1)
)
cinema[1][0] = 1
for (array in cinema) {
for (value in array) {
print(value)
}
println()
}
{/KOTLIN_CONSOLE}
Declaring an empty 2D array of a given size:
val cinema = arrayOf<Array<Int>>()
Would you like to learn more? A complete lesson on this topic follows.
In the previous lesson, Solved tasks for Kotlin lesson 9, we learned how to use the
split()
and joinToString()
methods. Today's Kotlin
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 the cinema in the picture)
Of course, a 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 Kotlin, we initialize a 2D array like this:
var cinema = arrayOf<Array<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 we still
have to fill it with zeros. We'll use two nested loops to do it. The outer one
will go through rows, the inner one will add the given number of zeros to each
row. We want the array to be of size 5x5
:
for (i in 0..4) { var array = arrayOf<Int>() for (j in 0..4) { array += 0 } cinema += 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 ") } println() }
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 for (i in 1..3) { cinema[3][i] = 1 } for (i in 0..4) { 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 = arrayOf<Array<Array<Int>>>()
We would fill it just like before:
for (i in 0..2) { var cinema = arrayOf<Array<Int>>() for (j in 0..4) { var array = arrayOf<Int>() for (k in 0..4) { array += 0 } cinema += array } cinemas += cinema }
The principle is still the same, it's just harder to comprehend
The code above creates the 3D array as 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) { println("Floor: $floor") floor += 1 for (array in cinema) { for (value in array) { print("$value ") } println() } println("-----------------") }
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 Kotlin, 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 627x (4.98 kB)