Lesson 1
Multidimensional Arrays and Their Traversal in Java
Topic Overview

Welcome to today's session on "Multidimensional Arrays and Their Traversal in Java". Multidimensional arrays are types of arrays that store arrays at each index instead of single elements. Picture it as an 'apartment building' with floors (the outer array) and apartments on each floor (the inner array). Our goal today is to strengthen your foundational knowledge of these 'apartment buildings' and how to handle them effectively in Java.

Creating Multidimensional Arrays

To construct a multidimensional array in Java, we use arrays of arrays. Here are examples to demonstrate how to create and work with 2D static arrays.

Java
1import java.util.Arrays; 2 3class Solution { 4 public static void main(String[] args) { 5 // Creating a 2D array 6 int[][] array = { 7 {1, 2, 3}, 8 {4, 5, 6}, 9 {7, 8, 9} 10 }; 11 12 // Printing the 2D array 13 System.out.println(Arrays.deepToString(array)); 14 } 15}
Indexing in Multidimensional Arrays

All indices in Java arrays are 0-based. Let's say you want to visit an apartment on the second floor (index 1) and bring a package to the first unit (index 0) in this building. Here's how you can do it:

Java
1class Solution { 2 public static void main(String[] args) { 3 int[][] array = { 4 {1, 2, 3}, 5 {4, 5, 6}, 6 {7, 8, 9} 7 }; 8 9 // Accessing an element 10 System.out.println(array[1][0]); // Outputs: 4 11 } 12}

We visited the element 4 in the array by its position. The number [1] refers to the second inner array, and [0] refers to the first element of that array.

Traversing Multidimensional Arrays

We can visit every floor (outer array) and every apartment on each floor (inner array) by using nested loops.

Java
1class Solution { 2 public static void main(String[] args) { 3 String[][] array = { 4 {"Apt 101", "Apt 102", "Apt 103"}, 5 {"Apt 201", "Exit Floor", "Apt 203"}, 6 {"Apt 301", "Apt 302", "Apt 303"} 7 }; 8 9 // Loop through 2D array 10 for (int i = 0; i < array.length; i++) { 11 for (int j = 0; j < array[i].length; j++) { 12 System.out.print(array[i][j] + ", "); 13 } 14 System.out.println(); 15 } 16 } 17} 18 19/* 20Prints: 21Apt 101, Apt 102, Apt 103, 22Apt 201, Exit Floor, Apt 203, 23Apt 301, Apt 302, Apt 303, 24*/
Updating Multidimensional Arrays

Continuing with the apartment-building analogy, suppose the task was to replace the old locker code (the second element in the first array) with a new one. Here's how we can achieve this:

Java
1class Solution { 2 public static void main(String[] args) { 3 // Defining and initializing the array 4 int[][] array = { 5 {1, 2, 3}, 6 {4, 5, 6}, 7 {7, 8, 9} 8 }; 9 10 // Updating an element 11 array[0][1] = 10; 12 for (int i = 0; i < array.length; i++) { 13 for (int j = 0; j < array[i].length; j++) { 14 System.out.print(array[i][j] + " "); 15 } 16 System.out.println(); 17 } 18 } 19}
Finding the Number of Rows and Columns

Java offers various ways to manage multidimensional arrays. For instance, you can determine the number of rows (floors) and columns (units on each floor):

Java
1class Solution { 2 public static void main(String[] args) { 3 int[][] array = { 4 {1, 2, 3}, 5 {4, 5, 6}, 6 {7, 8, 9} 7 }; 8 9 // Finding the number of rows 10 int numFloors = array.length; 11 System.out.println(numFloors); // Outputs: 3 12 13 // Finding the number of columns 14 int numUnits = array[0].length; 15 System.out.println(numUnits); // Outputs: 3 16 } 17}
Adding a New Row or Column

To add a new row or column to a 2D static array, create a new array with the desired dimensions, then copy the existing elements into it.

Adding a New Row

Java
1import java.util.Arrays; 2 3class Solution { 4 public static void main(String[] args) { 5 int[][] array = { 6 {1, 2, 3}, 7 {4, 5, 6}, 8 {7, 8, 9} 9 }; 10 11 // Adding a new row to our array 12 int[][] newArray = new int[array.length + 1][3]; 13 for (int i = 0; i < array.length; i++) { 14 newArray[i] = Arrays.copyOf(array[i], array[i].length); 15 } 16 newArray[3] = new int[]{10, 11, 12}; 17 18 for (int i = 0; i < newArray.length; i++) { 19 for (int j = 0; j < newArray[i].length; j++) { 20 System.out.print(newArray[i][j] + " "); 21 } 22 System.out.println(); 23 } 24 } 25}
Removing a Row or Column

To remove a row or column from a 2D static array, create a new array with the reduced dimensions and copy over the elements while skipping the one you want to remove.

Removing a Column

Java
1class Solution { 2 public static void main(String[] args) { 3 int[][] array = { 4 {1, 2, 3}, 5 {4, 5, 6}, 6 {7, 8, 9} 7 }; 8 9 // Removing the second column 10 int[][] newArray = new int[array.length][array[0].length - 1]; 11 for (int i = 0; i < array.length; i++) { 12 int index = 0; 13 for (int j = 0; j < array[i].length; j++) { 14 if (j != 1) { 15 newArray[i][index] = array[i][j]; 16 index++; 17 } 18 } 19 } 20 21 for (int i = 0; i < newArray.length; i++) { 22 for (int j = 0; j < newArray[i].length; j++) { 23 System.out.print(newArray[i][j] + " "); 24 } 25 System.out.println(); 26 } 27 } 28}
Break/Continue in Nested Loops

Sometimes, when we visit every apartment on each floor, we might need to start visiting the next floor midway. break helps us exit the current loop, while continue helps us skip the current iteration and move to the next one.

Java
1class Solution { 2 public static void main(String[] args) { 3 String[][] array = { 4 {"Apt 101", "Apt 102", "Apt 103"}, 5 {"Apt 201", "Exit Floor", "Apt 203"}, 6 {"Apt 301", "Apt 302", "Apt 303"} 7 }; 8 9 // Break in nested loop 10 for (int i = 0; i < array.length; i++) { 11 for (int j = 0; j < array[i].length; j++) { 12 if (array[i][j].equals("Exit Floor")) { 13 break; 14 } 15 System.out.print(array[i][j] + ", "); 16 } 17 System.out.println(); 18 } 19 } 20} 21 22/* 23Prints: 24Apt 101, Apt 102, Apt 103, 25Apt 201, 26Apt 301, Apt 302, Apt 303, 27*/

Here, as soon as Exit Floor is found on a floor, the entire loop breaks, and no further units on the floor are visited. However, the other units are processed as before, as break breaks only the nested loop.

We can also make use of continue in a similar scenario:

Java
1class Solution { 2 public static void main(String[] args) { 3 String[][] array = { 4 {"Apt 101", "Apt 102", "Apt 103"}, 5 {"Apt 201", "Exit Floor", "Apt 203"}, 6 {"Apt 301", "Apt 302", "Apt 303"} 7 }; 8 9 // Continue in nested loop 10 for (int i = 0; i < array.length; i++) { 11 for (int j = 0; j < array[i].length; j++) { 12 if (array[i][j].equals("Exit Floor")) { 13 continue; 14 } 15 System.out.print(array[i][j] + ", "); 16 } 17 System.out.println(); 18 } 19 } 20} 21 22/* 23Prints: 24Apt 101, Apt 102, Apt 103, 25Apt 201, Apt 203, 26Apt 301, Apt 302, Apt 303, 27*/

In this case, when Exit Floor is encountered, the continue statement is executed. This skips printing Exit Floor and continues with the next unit on the same floor. The loop doesn't stop entirely but skips over Exit Floor, resulting in a missing apartment in the printout for the second floor.

Lesson Summary and Practice

That was exciting! We went through various operations on multidimensional arrays, starting from their creation, methods to update, and useful Java methods. We also learned how we can visit every floor and every apartment on each floor.

Practice solidifies learning! Your new adventure awaits in our upcoming practical exercises, where you can apply these concepts to multidimensional arrays! Buckle up and have fun!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.