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.

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:

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.

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:

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:

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.

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.

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.

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:

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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal