Kotlin
Java

Multidimensional Arrays
Kotlin

Created By: Geoffrey Challen
/ Updated: 2021-09-13

Welcome back to CS 124! Today is another extremely exciting day. Because today, we'll learn how to work with even more data. We'll break free from our linear shackles into full multi-dimensional splendor. Let's get started!

Multidimensional Data

So far we've worked with single data values, arrays of values, and Strings—which on some level or just character arrays with features. But all of the plural data that we've worked with so far has been linear. We've learned how to put things in order. But just try linearizing this guy:

It turns out that a lot of the data around us is multidimensional. Photos are just one example.

Discuss some of your favorite examples of multidimensional data: photos, video, even audio.

Multidimensional Arrays

Of course Kotlin has a way to work with multidimensional data. In some ways it's a straightforward extension of what we've been doing, but in other ways in introduces some new syntax.

Here's our first multidimensional array:

We see both familiar and unfamiliar bits here. IntArray(8) is how we created an empty array of Int values. Array(8) looks similar, except it's not prefixed with a type. But what is happening with the rest of the syntax? Let's investigate!

Explain the array initializer syntax and why the outer array is not prefixed with Int.

How would we create an empty three-dimensional array?

Similar idea. The 3-d array shown above has size 8 in the first dimension, 88 in the second dimension, and 8 in the third dimension. And it contains Ints, since the inner array is an IntArray. If we wanted the same size array but containing Doubles, it would look like this:

Array indexing in multidimensional arrays works just the same as we've seen in the past:

And we can still have problems with our bounds if we're not careful:

Forget Rows and Columns

A bi-yearly rant. Forget about rows and columns. Do you want to work with spreadsheets your entire life? This limited mental model will utterly fail you when you need it most!

Rant about rows and colums.

Arrays of Arrays of Arrays

Let's explore how multidimensional arrays in Kotlin actually work. Specifically, we'll examine the following piece of code:

Discuss how Kotlin arrays are arrays of arrays, and the consequences for working with them. Present arrayOfNulls.

Non-Rectangular Arrays

Note one important consequence of the fact that Kotlin arrays are arrays of arrays. They do not need to be rectangular! As demonstrated above, an inner array can have a different size at each index. Some may even be null!

If this doesn't make perfect sense to you, don't worry. Next we'll show you patterns that you can use below to work with any array, rectangular or non.

Array Initializers

Let's look a bit more closely at the syntax for declaring multidimensional arrays in Kotlin. Specifically, let's start our investigation here:

What's going on? Didn't we previously say that arrays of Ints were initialized to zero? Why does this one seem to contain all 4s? And what's going on with the { block following the IntArray(8)?

That block contains what is called an array initializer. Each element in the array is initialized by evaluating that expression. Which can contain any expression that evaluates to an Int:

We won't use this feature often. But it's helpful to understand when examining Kotlin's syntax for working with multidimensional arrays. Specifically:

Initializes values to an array of 8 elements (Array(8)) where each of the 8 elements is the result of evaluating { IntArray(2) }, meaning each result is an IntArray(2), or an array of Ints of size 2. Whew!

Multidimensional Array Literals

These exist as a fairly straightforward extension of what we've seen already. But don't worry: we don't use these often!

The difficulty with this syntax is that it's hard if not impossible to tell what value ends up where by reading the initialization. That's why we tend to avoid this syntax.

Multidimensional Array Programming Patterns

Just like single-dimensional arrays, we can develop similar programming patterns for working with multidimensional arrays. Let's look at an example together.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough