Lists Review and Performance : 03/31/2023
Linked Lists : 03/30/2023
Algorithms and Lists : 03/29/2023
Lambda Expressions : 03/24/2023
Anonymous Classes : 03/23/2023
Practice with Interfaces : 03/22/2023
Implementing Interfaces : 03/21/2023
Using Interfaces : 03/20/2023
Working with Exceptions : 03/10/2023
Throwing Exceptions : 03/09/2023
Catching Exceptions : 03/08/2023
References and Polymorphism : 03/07/2023
References : 03/06/2023
Data Modeling 2 : 03/03/2023
Equality and Object Copying : 03/02/2023
Polymorphism : 03/01/2023
Inheritance : 02/28/2023
Data Modeling 1 : 02/27/2023
Static : 02/24/2023
Encapsulation : 02/23/2023
Constructors : 02/22/2023
Objects, Continued : 02/21/2023
Introduction to Objects : 02/20/2023
Compilation and Type Inference : 02/17/2023
Practice with Collections : 02/16/2023
Maps and Sets : 02/15/2023
Lists and Type Parameters : 02/14/2023
Imports and Libraries : 02/13/2023
Multidimensional Arrays : 02/10/2023
Practice with Strings : 02/09/2023
null : 02/08/2023
Algorithms and Strings : 02/07/2023
Strings : 02/06/2023
Functions and Algorithms : 02/03/2023
Practice with Functions : 02/02/2023
More About Functions : 02/01/2023
Errors and Debugging : 01/31/2023
Functions : 01/30/2023
Practice with Loops and Algorithms : 01/27/2023
Algorithms : 01/26/2023
Loops : 01/25/2023
Arrays : 01/24/2023
Compound Conditionals : 01/23/2023
Conditional Expressions and Statements : 01/20/2023
Operations on Variables : 01/19/2023
Variables and Types : 01/18/2023
Welcome to CS 124 : 01/17/2023
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;
}
}
System.out.println(pixels[8][18]);
This is another extremely exciting lesson.
Because now, 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!
But first, let’s get a bit more debugging practice!
So far we’ve worked with single data values, arrays of values, and String
s—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.
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];
System.out.println(values[4][2][1]);
values[2][2][1] = 10;
System.out.println(values[2][2][1]);
And we can still have problems with our bounds if we’re not careful:
int[][][] values = new int[8][4][2];
System.out.println(values[4][2][2]);
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];
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];
System.out.println(twod.length);
System.out.println(twod[0].length);
System.out.println(twod[1].length);
int[][] twod = new int[4][];
Note one important consequence of the fact that Java arrays are arrays of arrays.
They do not need to be rectangular!
Specifically, an innermost 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.
These exist, but they are awful.
We’ll never do this to you:
int[][][] values = new int[][][] {new int[][] {new int[] {1, 2}, new int[] {3}}};
System.out.println(values[0][1][0]);
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};
More Practice
Need more practice? Head over to the practice page.