Lesson 1
Mastering Array Traversal and Manipulation in C#
Topic Overview

Welcome to today's session on "Mastering Array Traversal and Manipulation in C#". 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 C#.

Creating Multidimensional Arrays

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

C#
1using System; 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 for (int i = 0; i < array.GetLength(0); i++) { 14 for (int j = 0; j < array.GetLength(1); j++) { 15 Console.Write(array[i, j] + " "); 16 } 17 Console.WriteLine(); 18 } 19 } 20} 21 22/* 23Prints: 241 2 3 254 5 6 267 8 9 27*/
Indexing in Multidimensional Arrays

All indices in C# 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:

C#
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 Console.WriteLine(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.

Finding the Number of Rows and Columns

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

C#
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.GetLength(0); 11 Console.WriteLine(numFloors); // Outputs: 3 12 13 // Finding the number of columns 14 int numUnits = array.GetLength(1); 15 Console.WriteLine(numUnits); // Outputs: 3 16 } 17}
Traversing Multidimensional Arrays

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

C#
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.GetLength(0); i++) { 11 for (int j = 0; j < array.GetLength(1); j++) { 12 Console.Write(array[i, j] + ", "); 13 } 14 Console.WriteLine(); 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:

C#
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.GetLength(0); i++) { 13 for (int j = 0; j < array.GetLength(1); j++) { 14 Console.Write(array[i, j] + " "); 15 } 16 Console.WriteLine(); 17 } 18 } 19} 20 21/* 22Prints: 231 10 3 244 5 6 257 8 9 26*/
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.

C#
1using System; 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 // Create a new array with an additional row 12 int[,] newArray = new int[array.GetLength(0) + 1, 3]; // Add 1 more row 13 14 // Copy existing array elements 15 for (int i = 0; i < array.GetLength(0); i++) { 16 for (int j = 0; j < array.GetLength(1); j++) { 17 newArray[i, j] = array[i, j]; 18 } 19 } 20 21 // Add new values to the extra row 22 newArray[3, 0] = 10; 23 newArray[3, 1] = 11; 24 newArray[3, 2] = 12; 25 26 // Display the new array 27 for (int i = 0; i < newArray.GetLength(0); i++) { 28 for (int j = 0; j < newArray.GetLength(1); j++) { 29 Console.Write(newArray[i, j] + " "); 30 } 31 Console.WriteLine(); 32 } 33 } 34} 35 36/* 37Prints: 381 2 3 394 5 6 407 8 9 4110 11 12 42*/
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.

C#
1using System; 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 // Removing the second row (index 1) 12 int[,] rowRemovedArray = new int[array.GetLength(0) - 1, array.GetLength(1)]; // New array with 1 less row 13 14 // Copy elements except the second row 15 int rowIndex = 0; 16 for (int i = 0; i < array.GetLength(0); i++) { 17 if (i != 1) { // Skip row 1 18 for (int j = 0; j < array.GetLength(1); j++) { 19 rowRemovedArray[rowIndex, j] = array[i, j]; 20 } 21 rowIndex++; 22 } 23 } 24 25 // Display the new array (with row removed) 26 for (int i = 0; i < rowRemovedArray.GetLength(0); i++) { 27 for (int j = 0; j < rowRemovedArray.GetLength(1); j++) { 28 Console.Write(rowRemovedArray[i, j] + " "); 29 } 30 Console.WriteLine(); 31 } 32 } 33} 34 35/* 36Prints: 371 2 3 387 8 9 39*/
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.

C#
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.GetLength(0); i++) { 11 for (int j = 0; j < array.GetLength(1); j++) { 12 if (array[i, j] == "Exit Floor") { 13 break; 14 } 15 Console.Write(array[i, j] + ", "); 16 } 17 Console.WriteLine(); 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:

C#
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.GetLength(0); i++) { 11 for (int j = 0; j < array.GetLength(1); j++) { 12 if (array[i, j] == "Exit Floor") { 13 continue; 14 } 15 Console.Write(array[i, j] + ", "); 16 } 17 Console.WriteLine(); 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 and methods of updating, and useful C# 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.