"Hello, my very smartest student!"

"Hello, Rishi!"

"Are you pining for a new discussion about arrays? Well, today we have one for you! Today I'm going to tell you about jagged and multidimensional arrays."

"Sounds bloodthirsty and frightening."

"Don't worry, a real programmer can always handle an array, even when it bares its teeth. Jokes aside, an array's jaggedness reflects the ability not only to swap the rows of a two-dimensional array, but also to construct an array however it needs to be.

"Let's say you want the first row of a two-dimensional array to have a length of 10, and the second to be 50."

"Can you really do that?"

"Absolutely! First, we create a 'container of containers' — this is the first array, which will store references to arrays of rows. This is how it's done:

``int[][] name = new int[height][];``

"You simply omit the second dimension, and the Java machine creates a container of containers. This is what will be in memory after executing this code:

"And, well, you already know how to create one-dimensional arrays 🙂

This is what the resulting code will look like:

 ``````// Matrix of important data int[][] matrix = new int[]; matrix = new int; matrix = new int`````` Two-dimensional array The zeroth row is an array of `10` elements The first row is an array of `50` elements

"We have just created a so-called "jagged" array. Its edges are rough and irregular.

"And if we now want to display all the elements of this array on the screen, then the array's `length` array will come in handy: after all, the lengths of the array's rows are different.

"By the way, can you tell me how to find the length of a 'container of containers' in our example? It is also an array object, which means that it has a length."

"Probably `matrix.length`?"

"Quite right! And for the arrays that form the rows, we would use `matrix.length` for the zeroth row."

"And for the first, that means we would use `matrix.length`?"

"Quite right. In the first case, the executing the command will yield 10, and in the second case, the result will be 50.

6
New Java Syntax, level 6, lesson 6
Locked
Triangular array
Create a triangular array where the value of each element is the sum of its indices. For example: array = 7 + 3 = 10, array = 3 + 0 = 3. Display the array in the following form: 0 1 2 2 3 4 3 4 5 6 4 5 6 7 8 5 6 7 8 9 10 ... The numbers in each line are separated by a space. You can def

## Working with a two-dimensional array

"Now let's try to display a two-dimensional array:

 ``````int[][] matrix = new int[]; matrix = {1, 2, 3, 4, 5, 6}; matrix = {1, 2, 3}; matrix = {1}; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) System.out.print( matrix[i][j] + " " ); System.out.println(); }`````` Create an array Fill the array with values Outer loop that iterators over the rows of the array. Inner loop that iterates over the cells of a single row.

"As you can see, we need two nested loops. The first we call outer, and the second — inner.

"In the outer loop (the `i` variable), we sequentially go through all the rows (arrays) that make up our two-dimensional array. Each value of `i` corresponds to a row with that index.

"In the inner loop (the `j` variable), we iterate over all the cells in the rows. Thanks to the inner loop, a row, which consists of the values of one one-dimensional array, will be displayed on the screen.

"This is what will be displayed:

 One line of the array is processed `1 2 3 4 5 6` Two lines of the array are processed ```1 2 3 4 5 6 1 2 3``` Three lines of the array are processed ```1 2 3 4 5 6 1 2 3 1```
6
New Java Syntax, level 6, lesson 6
Locked
Creating a two-dimensional array
A two-dimensional array is an array of arrays. That is, an array where each cell holds a reference to an array. But it is much easier to think of it as a table that has a number of rows (first dimension) and a number of columns (second dimension). In this task, we will create such an array. Impleme

## Multidimensional arrays

"Amigo! Did you guess that if there are two-dimensional arrays, then there can also be three-dimensional ones?

"Yes, you can create a three-dimensional array, and in general, an array of any dimension. Such arrays are called 'multidimensional'. Just for fun, let's create a multidimensional array that has 4 dimensions.

`` int[][][][] matrix = new int;``

"That doesn't seem very difficult!"

"You haven't tried to create one manually yet! Here, feast your eyes on this:

``````int[][][][] matrix;
matrix = new int[][][];                   // Create a 2-element array of references to references to references
for (int i = 0; i < matrix.length; i++)
{
matrix[i] = new int[][];                // Create a 3-element array of references to references
for (j = 0; j < matrix[i].length; j++)
{
matrix[i][j] = new int[];             // Create a 4-element array of references
for (k = 0; k < matrix[i][j].length; k++)
matrix[i][j][k] = new int;          // Create 5-element arrays of integers
}
}``````

"And that's just creating an array! Then you also need to work with it somehow."

"I take back what I said. It's not so easy to work with them. But it is possible."

"Since it's possible, here's a bonus task. Write code that displays all the values in a three-dimensional array. You know enough to do this. The main thing is to be patient and attentive. Or maybe being resourceful (there's a secret bit of knowledge that will help you solve this task in a single line). But no matter how you solve it, solve it."

"Thank you, Rishi. I'll try."

6