Welcome to Lesson 9! In this lesson, we’ll explore multidimensional arrays in C#, which are an extension of the basic array concept. Multidimensional arrays allow you to store data in a grid-like structure, enabling you to work with more complex data arrangements.
1. What is a Multidimensional Array?
A multidimensional array is an array of arrays. It allows you to store data in two or more dimensions, such as rows and columns in a grid. In C#, the most common multidimensional arrays are 2D arrays, but you can also work with higher dimensions.
2. Declaring and Initializing 2D Arrays
A 2D array can be thought of as a table or matrix with rows and columns. You declare a 2D array with two dimensions and initialize it with specific sizes.
Syntax:
type[,] arrayName = new type[rows, columns];
Example:
int[,] matrix = new int[3, 4]; // A 3x4 matrix
Explanation:
- This creates a 2D array named
matrix
with 3 rows and 4 columns. All elements are initialized to0
.
Initializing a 2D Array with Values:
You can also initialize a 2D array with values at the time of declaration.
Syntax:
type[,] arrayName =
{
{ value11, value12, value13, ... },
{ value21, value22, value23, ... },
{ value31, value32, value33, ... },
...
};
Example:
int[,] matrix =
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
Explanation:
- This initializes a 3×4 matrix with specified values.
3. Accessing Elements in a 2D Array
You access elements in a 2D array using two indices: one for the row and one for the column.
Example:
int[,] matrix =
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
Console.WriteLine(matrix[0, 0]); // Output: 1
Console.WriteLine(matrix[1, 2]); // Output: 7
Console.WriteLine(matrix[2, 3]); // Output: 12
Explanation:
matrix[0, 0]
accesses the element in the first row and first column, which is 1.matrix[1, 2]
accesses the element in the second row and third column, which is 7.matrix[2, 3]
accesses the element in the third row and fourth column, which is 12.
4. Iterating Through a 2D Array
You can use nested loops to iterate through each element of a 2D array.
Example Using for
Loops:
int[,] matrix =
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
for (int i = 0; i < matrix.GetLength(0); i++) // Loop through rows
{
for (int j = 0; j < matrix.GetLength(1); j++) // Loop through columns
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
Explanation:
matrix.GetLength(0)
returns the number of rows.matrix.GetLength(1)
returns the number of columns.- The nested
for
loops iterate through each element, printing it with a space.
5. Working with Higher-Dimensional Arrays
C# supports arrays with more than two dimensions, though they are less common. You declare them similarly to 2D arrays but with additional dimensions.
Syntax:
type[,,] arrayName = new type[x, y, z];
Example (3D Array):
int[,,] threeDArray = new int[2, 3, 4];
Explanation:
- This creates a 3D array with 2 layers, each containing a 3×4 matrix.
Initializing a 3D Array with Values:
int[,,] threeDArray =
{
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
},
{
{ 13, 14, 15, 16 },
{ 17, 18, 19, 20 },
{ 21, 22, 23, 24 }
}
};
Explanation:
- This initializes a 3D array with 2 layers, each containing a 3×4 matrix of values.
6. Example: Transposing a 2D Array
Transposing a 2D array means converting rows into columns and vice versa.
Example:
int[,] original =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
int[,] transposed = new int[3, 2];
for (int i = 0; i < original.GetLength(0); i++)
{
for (int j = 0; j < original.GetLength(1); j++)
{
transposed[j, i] = original[i, j];
}
}
for (int i = 0; i < transposed.GetLength(0); i++)
{
for (int j = 0; j < transposed.GetLength(1); j++)
{
Console.Write(transposed[i, j] + " ");
}
Console.WriteLine();
}
Explanation:
- The code transposes the
original
matrix, swapping rows and columns, and prints thetransposed
matrix.
Summary
In this lesson, we covered multidimensional arrays in C#, focusing on 2D arrays. You learned how to declare, initialize, and access elements in 2D arrays, as well as how to iterate through them using nested loops. We also briefly touched on higher-dimensional arrays and provided an example of transposing a 2D array.
Multidimensional arrays are powerful tools for handling complex data structures and can be used in a variety of applications.
Happy coding!
0 Comments