Kotlin
Java

Multidimensional Arrays
Java

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 Java has a way to work with multidimensional data. And, in many ways, it's a straightforward extension of what we've already seen.

Here's our first multidimensional array:

The syntax is similar to what we saw with single-dimensional arrays. But instead of a single [] in the variable declaration, we have two, indicating a two-dimensional array. How would we do three?

Same idea. Also note that on the right side of the initial assignment we can specify sizes for each of the dimensions. The 3-d array shown above has size 8 in the first dimension, 88 in the second dimension, and 8 in the third dimension.

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 Java actually work. Specifically, we'll talk about why something like this works:

Discuss how Java arrays are arrays of arrays, and the consequences for working with them.

Non-Rectangular Arrays

Note one important consequence of the fact that Java arrays are arrays of arrays. They do not need to be rectangular! Specifically, an inner array can have a different size at each index. Some may even be null! Let's look at how.

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.

Multidimensional Array Literals

These exist, but they are awful. We'll never do this to you:

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