Lesson 1
Multidimensional Arrays and Their Traversal in Kotlin
Topic Overview

Welcome to today's session on "Multidimensional Arrays and Their Traversal in Kotlin". Multidimensional arrays in Kotlin are arrays that can hold other arrays as their elements. Imagine them as an 'apartment building' with floors (the outer array) and apartments on each floor (the inner array). Our goal today is to strengthen your foundational knowledge of these 'apartment buildings' and how to handle them effectively in Kotlin.

Creating Multidimensional Arrays

In Kotlin, we use arrays of arrays to construct a multidimensional array. Here are examples demonstrating how to create and work with 2D static arrays.

Kotlin
1fun main() { 2 // Creating a 2D array 3 val array = arrayOf( 4 arrayOf(1, 2, 3), 5 arrayOf(4, 5, 6), 6 arrayOf(7, 8, 9) 7 ) 8 9 println(array.contentDeepToString()) // Outputs [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 10}
Indexing in Multidimensional Arrays

In Kotlin, all indices in arrays are 0-based. If you want to visit an apartment on the second floor (index 1) and bring a package to the first unit (index 0) in this building, you would do:

Kotlin
1fun main() { 2 val array = arrayOf( 3 arrayOf(1, 2, 3), 4 arrayOf(4, 5, 6), 5 arrayOf(7, 8, 9) 6 ) 7 8 // Accessing an element 9 println(array[1][0]) // Outputs: 4 10}

We accessed the element 4 in the array using its position. The number [1] refers to the second inner array, and [0] refers to the first element of that array.

Traversing Multidimensional Arrays

In Kotlin, you can visit every floor (outer array) and every apartment on each floor (inner array) using nested loops.

Kotlin
1fun main() { 2 val array = arrayOf( 3 arrayOf("Apt 101", "Apt 102", "Apt 103"), 4 arrayOf("Apt 201", "Exit Floor", "Apt 203"), 5 arrayOf("Apt 301", "Apt 302", "Apt 303") 6 ) 7 8 // Loop through 2D array 9 for (floor in array) { 10 for (apartment in floor) { 11 print("$apartment, ") 12 } 13 println() 14 } 15 /* 16 Expected output: 17 Apt 101, Apt 102, Apt 103, 18 Apt 201, Exit Floor, Apt 203, 19 Apt 301, Apt 302, Apt 303, 20 */ 21}
Updating Multidimensional Arrays

Continuing with the apartment-building analogy, suppose the task was to replace the old locker code (the second element in the first array) with a new one. Here's how we can achieve this in Kotlin:

Kotlin
1fun main() { 2 // Defining and initializing the array 3 val array = arrayOf( 4 arrayOf(1, 2, 3), 5 arrayOf(4, 5, 6), 6 arrayOf(7, 8, 9) 7 ) 8 9 // Updating an element 10 array[0][1] = 10 11 for (floor in array) { 12 for (unit in floor) { 13 print("$unit ") 14 } 15 println() 16 } 17 /* 18 Expected output: 19 1 10 3 20 4 5 6 21 7 8 9 22 */ 23}
Finding the Number of Rows and Columns

Kotlin provides a straightforward way to manage multidimensional arrays. For instance, you can determine the number of rows (floors) and columns (units on each floor) with:

Kotlin
1fun main() { 2 val array = arrayOf( 3 arrayOf(1, 2, 3), 4 arrayOf(4, 5, 6), 5 arrayOf(7, 8, 9) 6 ) 7 8 // Finding the number of rows 9 val numFloors = array.size 10 println(numFloors) // Outputs: 3 11 12 // Finding the number of columns 13 val numUnits = array[0].size 14 println(numUnits) // Outputs: 3 15}
Adding a New Row or Column

To add a new row or column to a 2D array in Kotlin, create a new array with the desired dimensions and copy the existing elements into it.

Kotlin
1fun main() { 2 val array = arrayOf( 3 arrayOf(1, 2, 3), 4 arrayOf(4, 5, 6), 5 arrayOf(7, 8, 9) 6 ) 7 8 // Adding a new row to our array 9 val newArray = array + arrayOf(10, 11, 12) 10 11 for (floor in newArray) { 12 for (unit in floor) { 13 print("$unit ") 14 } 15 println() 16 } 17 /* 18 Expected output: 19 1 2 3 20 4 5 6 21 7 8 9 22 10 11 12 23 */ 24}
Removing a Row or Column

To remove a row or column from a 2D static array in Kotlin, create a new array with the reduced dimensions and copy over the elements while skipping the one you want to remove.

Kotlin
1fun main() { 2 val array = arrayOf( 3 arrayOf(1, 2, 3), 4 arrayOf(4, 5, 6), 5 arrayOf(7, 8, 9) 6 ) 7 8 // Removing the second column 9 val newArray = array.map { row -> 10 row.filterIndexed { index, _ -> index != 1 }.toTypedArray() 11 }.toTypedArray() 12 13 for (floor in newArray) { 14 for (unit in floor) { 15 print("$unit ") 16 } 17 println() 18 } 19 /* 20 Expected output: 21 1 3 22 4 6 23 7 9 24 */ 25}
Break/Continue in Nested Loops

Sometimes, when visiting every apartment on each floor, we might need to start visiting the next floor midway. break helps us exit the current loop, while continue helps us skip the current iteration and move to the next one.

Kotlin
1fun main() { 2 val array = arrayOf( 3 arrayOf("Apt 101", "Apt 102", "Apt 103"), 4 arrayOf("Apt 201", "Exit Floor", "Apt 203"), 5 arrayOf("Apt 301", "Apt 302", "Apt 303") 6 ) 7 8 // Break in nested loop 9 for (floor in array) { 10 for (apartment in floor) { 11 if (apartment == "Exit Floor") { 12 break 13 } 14 print("$apartment, ") 15 } 16 println() 17 } 18 /* 19 Expected output: 20 Apt 101, Apt 102, Apt 103, 21 Apt 201, 22 Apt 301, Apt 302, Apt 303, 23 */ 24}

Here, as soon as Exit Floor is found on a floor, the inner loop breaks, skipping further units on that floor. However, the outer loop continues as break affects only the nested loop.

Similarly, you can use continue in a similar scenario:

Kotlin
1fun main() { 2 val array = arrayOf( 3 arrayOf("Apt 101", "Apt 102", "Apt 103"), 4 arrayOf("Apt 201", "Exit Floor", "Apt 203"), 5 arrayOf("Apt 301", "Apt 302", "Apt 303") 6 ) 7 8 // Continue in nested loop 9 for (floor in array) { 10 for (apartment in floor) { 11 if (apartment == "Exit Floor") { 12 continue 13 } 14 print("$apartment, ") 15 } 16 println() 17 } 18 /* 19 Expected output: 20 Apt 101, Apt 102, Apt 103, 21 Apt 201, Apt 203, 22 Apt 301, Apt 302, Apt 303, 23 */ 24}

In this example, when Exit Floor is encountered, the continue statement skips the printing of Exit Floor and proceeds with the next unit on the same floor. The loop doesn't stop entirely but skips over Exit Floor, resulting in a missing apartment in the printout for the second floor.

Lesson Summary and Practice

What an exciting journey! We explored various operations on multidimensional arrays, starting from their creation, updating elements, and efficient techniques to traverse them using Kotlin. We also learned how to visit every floor and every apartment on each floor.

Practice solidifies your learning! Your new adventure awaits in our upcoming practical exercises, where you can apply these concepts to multidimensional arrays! Buckle up and have fun with Kotlin!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.