12 minlesson

Multidimensional Arrays

Multidimensional Arrays

C# supports arrays with more than one dimension. This lesson covers 2D arrays, rectangular arrays, and jagged arrays.

2D Arrays (Rectangular)

A 2D array is like a table with rows and columns:

csharp
1// Declare a 3x4 array (3 rows, 4 columns)
2int[,] matrix = new int[3, 4];
3
4// Access elements with [row, column]
5matrix[0, 0] = 1; // First row, first column
6matrix[1, 2] = 5; // Second row, third column
7matrix[2, 3] = 9; // Third row, fourth column

Initialize with Values

csharp
1int[,] grid = {
2 { 1, 2, 3 },
3 { 4, 5, 6 },
4 { 7, 8, 9 }
5};
6
7Console.WriteLine(grid[0, 0]); // 1
8Console.WriteLine(grid[1, 1]); // 5
9Console.WriteLine(grid[2, 2]); // 9

Getting Dimensions

csharp
1int[,] matrix = new int[3, 4];
2
3int rows = matrix.GetLength(0); // 3
4int columns = matrix.GetLength(1); // 4
5int total = matrix.Length; // 12 (total elements)

Iterating Over 2D Arrays

csharp
1int[,] grid = {
2 { 1, 2, 3 },
3 { 4, 5, 6 },
4 { 7, 8, 9 }
5};
6
7// Using nested for loops
8for (int row = 0; row < grid.GetLength(0); row++)
9{
10 for (int col = 0; col < grid.GetLength(1); col++)
11 {
12 Console.Write($"{grid[row, col]} ");
13 }
14 Console.WriteLine();
15}
16
17// Using foreach (iterates all elements, no index)
18foreach (int value in grid)
19{
20 Console.Write($"{value} ");
21}

Practical 2D Array Example

Multiplication Table

csharp
1int size = 10;
2int[,] table = new int[size, size];
3
4// Fill the table
5for (int i = 0; i < size; i++)
6{
7 for (int j = 0; j < size; j++)
8 {
9 table[i, j] = (i + 1) * (j + 1);
10 }
11}
12
13// Print the table
14Console.WriteLine("Multiplication Table:");
15Console.Write(" ");
16for (int j = 1; j <= size; j++)
17{
18 Console.Write($"{j,4}");
19}
20Console.WriteLine();
21Console.WriteLine(new string('-', 4 + size * 4));
22
23for (int i = 0; i < size; i++)
24{
25 Console.Write($"{i + 1,3}|");
26 for (int j = 0; j < size; j++)
27 {
28 Console.Write($"{table[i, j],4}");
29 }
30 Console.WriteLine();
31}

Tic-Tac-Toe Board

csharp
1char[,] board = {
2 { 'X', 'O', 'X' },
3 { 'O', 'X', 'O' },
4 { 'O', 'X', 'X' }
5};
6
7void PrintBoard()
8{
9 Console.WriteLine(" 0 1 2");
10 for (int row = 0; row < 3; row++)
11 {
12 Console.Write($"{row} ");
13 for (int col = 0; col < 3; col++)
14 {
15 Console.Write($" {board[row, col]} ");
16 if (col < 2) Console.Write("|");
17 }
18 Console.WriteLine();
19 if (row < 2) Console.WriteLine(" ---+---+---");
20 }
21}
22
23PrintBoard();

3D Arrays

For three-dimensional data:

csharp
1// 2x3x4 array (like 2 tables of 3 rows × 4 columns)
2int[,,] cube = new int[2, 3, 4];
3
4cube[0, 0, 0] = 1;
5cube[1, 2, 3] = 100;
6
7// Iterate
8for (int i = 0; i < cube.GetLength(0); i++)
9{
10 for (int j = 0; j < cube.GetLength(1); j++)
11 {
12 for (int k = 0; k < cube.GetLength(2); k++)
13 {
14 Console.Write($"[{i},{j},{k}]={cube[i,j,k]} ");
15 }
16 Console.WriteLine();
17 }
18 Console.WriteLine("---");
19}

Jagged Arrays

Jagged arrays are "arrays of arrays" where each row can have different lengths:

csharp
1// Declare jagged array
2int[][] jagged = new int[3][];
3
4// Initialize each row with different sizes
5jagged[0] = new int[] { 1, 2 };
6jagged[1] = new int[] { 3, 4, 5, 6 };
7jagged[2] = new int[] { 7, 8, 9 };
8
9// Access elements
10Console.WriteLine(jagged[0][1]); // 2
11Console.WriteLine(jagged[1][3]); // 6

Initialize Inline

csharp
1int[][] triangle = {
2 new[] { 1 },
3 new[] { 1, 1 },
4 new[] { 1, 2, 1 },
5 new[] { 1, 3, 3, 1 },
6 new[] { 1, 4, 6, 4, 1 }
7};
8
9// Print Pascal's Triangle
10foreach (int[] row in triangle)
11{
12 foreach (int num in row)
13 {
14 Console.Write($"{num} ");
15 }
16 Console.WriteLine();
17}

Iterating Jagged Arrays

csharp
1int[][] data = {
2 new[] { 1, 2, 3 },
3 new[] { 4, 5 },
4 new[] { 6, 7, 8, 9 }
5};
6
7for (int i = 0; i < data.Length; i++)
8{
9 Console.Write($"Row {i}: ");
10 for (int j = 0; j < data[i].Length; j++)
11 {
12 Console.Write($"{data[i][j]} ");
13 }
14 Console.WriteLine();
15}

Rectangular vs Jagged

FeatureRectangular [,]Jagged [][]
Memory layoutContiguousSeparate arrays
Row sizesMust be equalCan vary
Syntaxarr[i, j]arr[i][j]
PerformanceSlightly fasterMore flexible
Use caseGrids, matricesVarying row lengths

When to Use Each

csharp
1// Rectangular: Fixed grids, game boards, matrices
2int[,] chessBoard = new int[8, 8];
3double[,] imagePixels = new double[1920, 1080];
4
5// Jagged: Variable row lengths, nested data
6string[][] menuItems = {
7 new[] { "File", "Edit", "View" },
8 new[] { "New", "Open", "Save", "Close" },
9 new[] { "Cut", "Copy", "Paste" }
10};

Practical Example: Seating Chart

csharp
1// Different rows have different numbers of seats
2string[][] theater = {
3 new[] { "A1", "A2", "A3", "A4", "A5" },
4 new[] { "B1", "B2", "B3", "B4", "B5", "B6" },
5 new[] { "C1", "C2", "C3", "C4", "C5", "C6", "C7" },
6 new[] { "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8" }
7};
8
9bool[][] occupied = {
10 new[] { true, false, false, true, false },
11 new[] { false, false, true, true, false, false },
12 new[] { true, true, true, false, false, false, false },
13 new[] { false, false, false, false, true, true, false, false }
14};
15
16Console.WriteLine("Theater Seating (X = taken, O = available):");
17Console.WriteLine();
18
19for (int row = 0; row < theater.Length; row++)
20{
21 Console.Write($"Row {(char)('A' + row)}: ");
22 for (int seat = 0; seat < theater[row].Length; seat++)
23 {
24 char status = occupied[row][seat] ? 'X' : 'O';
25 Console.Write($"[{status}] ");
26 }
27 Console.WriteLine();
28}
29
30// Count available seats
31int available = 0;
32for (int row = 0; row < occupied.Length; row++)
33{
34 for (int seat = 0; seat < occupied[row].Length; seat++)
35 {
36 if (!occupied[row][seat])
37 available++;
38 }
39}
40Console.WriteLine($"\nAvailable seats: {available}");

Summary

In this lesson, you learned:

  • 2D rectangular arrays use [,] syntax with fixed dimensions
  • Access elements with array[row, column]
  • Use GetLength(dimension) to get dimension sizes
  • Jagged arrays [][] allow varying row lengths
  • Choose rectangular for fixed grids, jagged for variable structures
  • Nested loops iterate over multidimensional arrays

Next, we'll explore the List collection for dynamic arrays.