CodeGym /Courses /Java Syntax Zero /Jagged arrays in Java

Jagged arrays in Java

Java Syntax Zero
Level 6 , Lesson 6
Available

1. Jagged arrays

As a Java programmer you can not only swap the rows of a two-dimensional array, but also construct an array however you want.

Let's say you want the first row of a two-dimensional array to have a length of 10, and you want the length of the second row to be 50. Can we do that? Yes, we can.

First, we need to 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:

Jagged arrays in Java

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[2][];
matrix[0] = new int[10];
matrix[1] = new int[50]
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".

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

By the way, how do you find the length of a 'container of containers' in our example? It is also an array object, which means that it has a length. The correct answer is matrix.length.

How about for the arrays that comprise our rows? matrix[0].length


6
Task
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] = 7 + 3 = 10, array[3][0] = 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

2. Working with a two-dimensional array

Suppose you want to display a two-dimensional array. How do you do that?

Our code will look something like this:

int[][] matrix = new int[3][];
matrix[0] = new int[]{1, 2, 3, 4, 5, 6};
matrix[1] = new int[]{1, 2, 3};
matrix[2] = new int[]{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.

You 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 row of the array is processed
1 2 3 4 5 6
Two rows of the array are processed
1 2 3 4 5 6
1 2 3
Three rows of the array are processed
1 2 3 4 5 6
1 2 3
1

6
Task
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

3. Multidimensional arrays

One more interesting fact about arrays, one that you've probably already guessed. If you can make a two-dimensional array, then can you make a three-dimensional array?

Yes, you can create 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[2][3][4][5];

This code is too simple, isn't it?

What if you create it manually?

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

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

Bonus task: write code that displays all the values in a three-dimensional array.


6
Task
New Java Syntax, level 6, lesson 6
Locked
Creating a multi-array
In the main(String[]) method, display all the numbers in the three-dimensional multiArray array.
Comments (17)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Mirna C. Level 16, United States of America, USA
29 January 2025
My brain is stuck in an infinite loop of multidimensional arrays....🥴...LOL!
Anonymous #11475584 Level 11, Hayward, United States
12 April 2024
WHAT WAS THATTTTT
Jesú Level 14, Madrid, Spain
7 March 2024
Arrays are one of the hardest things to grasp when you start on your coding journey. You need to practice with hundreds of cases in order to truly grasp them.
haalk3n Level 6, Romania
16 February 2024
For anyone who thinks this is easy, who the hell taught you?? I NEED to know.
Shanthi Sri Level 64, CodeGym University in India, India Expert
13 September 2023
Yes, its so diiffcult to solve the probelm
Campbell, Jackson Level 12, Corona, USA
12 October 2023
Light work no reaction
Acuna, Landon Level 6, United States of America, USA
8 December 2023
this is not easy!
Nguyen, Louis Level 10, Corona, USA
31 August 2023
I assumed the 3d array was literally in three dimensions which confused me, but it turns out it's just a collection (or an array) of 2d arrays, which I think is much simpler to understand
Campbell, Jackson Level 12, Corona, USA
12 October 2023
actually light
Sawyee Level 16, United States
24 August 2023
Beware all ye who enter. This was the hardest lesson yet for me. Kind of scared to click unlock next lesson...oh well here goes nothin' wish me luck!
larsintech Level 16, Switzerland Expert
31 July 2023
My brain is literally on fire rn
Mohit Soni Level 9, CodeGym University in India, India
17 March 2023
I have jagged arrays roaming in my mind after solving this.
Altan Şengül Level 9, Turkey
21 February 2023
Arrays are so difficult to understand idk why :)