C# Multidimensional Arrays
In this chapter you will learn:
- What are C# Multidimensional Arrays
- Declare, create, and initialize the rectangular array
- Rectangular arrays
- Jagged arrays
- How to get array rank for two-dimensional array
Description
Multidimensional arrays come in two varieties: rectangular and jagged.
Rectangular arrays represent an n-dimensional block of memory, and jagged arrays are arrays of arrays.
Rectangular Array Jagged Array
1 2 3 1 2
4 5 6 3 4 5 6 7 8
7 8 9 9 10 11
Syntax
The general form of a multidimensional array declaration:
type[, ...,] name = new type[size1, size2, ..., sizeN];
The following code declares a Two-Dimensional Array.
class MainClass/*from w w w . ja v a 2s.c o m*/
{
static void Main()
{
int[,] cells = new int[3,3];
}
}
The general form of array initialization for a two-dimensional array is shown here:
type[,] array_name = {// w w w. ja va2s .c o m
{ val, val, val, ..., val },
{ val, val, val, ..., val },
.
.
.
{ val, val, val, ..., val }
};
The following code shows how to declare and initialize an three-dimensional array.
using System;/*from w w w. j a v a2 s. c o m*/
class MainClass
{
static void Main()
{
int[,] arr = new int[2, 3] { { 0, 1, 2 }, { 10, 11, 12 } };
for (int i = 0; i < 2; i++){
for (int j = 0; j < 3; j++){
Console.WriteLine(arr[i, j]);
}
}
}
}
The code above generates the following result.
Rectangular arrays
Rectangular arrays are declared using commas to separate each dimension.
The following declares a rectangular two-dimensional array, where the dimensions are 3 by 3:
int [,] matrix = new int [3, 3];
The GetLength method of an array returns the length for a given dimension (starting at 0):
for (int i = 0; i < matrix.GetLength(0); i++) {
for (int j = 0; j < matrix.GetLength(1); j++) {
matrix [i, j] = i * 3 + j;//from ww w . j av a 2s . co m
}
}
A rectangular array can be initialized as follows:
int[,] matrix = new int[,]
{ /* ww w. j a v a 2 s . co m*/
{0,1,2},
{3,4,5},
{6,7,8}
};
Jagged arrays
Jagged arrays are declared using successive square brackets to represent each dimension.
Here is an example of declaring a jagged two-dimensional array, where the outermost dimension is 3:
int [][] jagged = new int [3][];
The inner dimensions aren't specified in the declaration.
Each inner array is implicitly initialized to null rather than an empty array.
Each inner array must be created manually:
int[][] jagged = new int[3][];
jagged[0] = new int[2] { 1, 2 };
jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 };
jagged[2] = new int[3] { 9, 10, 11 };
A jagged array can be initialized as follows:
int[][] matrix = new int[][]
{ //from w w w .j av a 2 s . co m
new int[] {0,1,2},
new int[] {3,4,5},
new int[] {6,7,8}
};
Example 1
using System;//from w ww .ja v a 2s . c om
class MainClass
{
public static void Main()
{
int[,,] intArray = new int [10, 5, 3];
intArray[1, 3, 2] = 3;
intArray[4, 1, 2] = 9;
Console.WriteLine("intArray.Rank (number of dimensions) = " +
intArray.Rank);
Console.WriteLine("intArray.Length (number of elements) = " +
intArray.Length);
for (int x = 0; x < intArray.GetLength(0); x++){
for (int y = 0; y < intArray.GetLength(1); y++){
for (int z = 0; z < intArray.GetLength(2); z++){
if (intArray[x, y, z] != 0){
Console.WriteLine("intArray[" + x + ", " + y + ", " + z +"] = " +
intArray[x, y, z]);
}
}
}
}
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- What is C# Array Initialization
- How to do C# Array Initialization
- Default Element Initialization
- Simplified Array Initialization Expressions