Kotlinlearncs.online LogoJava

Multidimensional Arrays

int[][] pixels = new int[32][32];
for (int i = 0; i < pixels.length; i++) {
for (int j = 0; j < pixels[i].length; j++) {
pixels[i][j] = i + j;

This is another extremely exciting lesson, because 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!

Debugging Practice
Debugging Practice

But first, let’s get a bit more debugging practice!

Write a function called reformatPhoneNumber. It should take a String containing a phone number in the format 111-222-3333 and return it reformatted as (111) 222-3333.

You will want to explore the various String methods to help you with this task. In particular, you may find split and substring helpful. There are solutions that use split, others that use substring, and probably others that use neither!

Multidimensional Data
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.

Multidimensional Arrays
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:

int[][] values = new int[8][8];

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?

int[][][] values = new int[8][88][8];

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:

int[][][] values = new int[8][4][2];
values[2][2][1] = 10;

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

int[][][] values = new int[8][4][2];

Forget Rows and Columns
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!

int[][] samples = new int[2][64];

Arrays of Arrays of Arrays
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:

int[][] twod = new int[2][];
int[] oned = new int[8];
twod[1] = oned;
twod[0] = new int[4];
int[][] twod = new int[4][];

Non-Rectangular Arrays
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.

int[][] nonrectangular = new int[8][];

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
Multidimensional Array Literals

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

// Holy terrible syntax, Batman!
int[][][] values = new int[][][] {new int[][] {new int[] {1, 2}, new int[] {3}}};

Practice: Array Sum (Two Dimensional)

Created By: Geoffrey Challen
/ Version: 2020.9.0

Declare and implement a function called arraySum that receives a two-dimensional array of double values as its only parameter and returns the sum of the values in the array as a double. If the array is null you should return 0. None of the inner arrays will be null.

Multidimensional Array Programming Patterns
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.

int[] values = {1, 2, 4};

Homework: 2D Array Max Subarray Sum

Created By: Geoffrey Challen
/ Version: 2021.8.0

Write a method maxSubarraySum that, given a non-rectangular two-dimensional int array, returns the sum of the subarray that sums to the largest value.

So given the following array, with each subarray on a separate line:

1, 2, 4
4, 1, -1
6, 8, -10, -9

You would return 7.

assert that the passed array is not null. However, if the passed array is not null it will contain no empty subarrays.

One hint for this problem is that you may need both an int variable to store the max and a boolean variable to record whether the maximum value has been initialized. Once you have summed each subarray, check whether either your boolean value is false or the sum is larger than the largest you've seen so far. After you check the sum of the first subarray, set your boolean value to true. Another approach is to use the counter that you use to proceed through each subarray to determine whether you have initialized the max value.

More Practice

Need more practice? Head over to the practice page.