Professional Documents
Culture Documents
Multidimensional ARRAY
&JAGGED ARRAY
1
Multidimensional
Arrays
2
Multi-dimensional ARRAY
C# supports multidimensional arrays up to 32 dimensions.
The multidimensional array can be declared by adding commas in the square brackets.
For example,
array [ , ] declares two-dimensional
array [ , , ] declares three-dimensional
array [ , , , ] declares four-dimensional array,
and so on.
So, in a multidimensional array,
no of commas = No of Dimensions - 1.
Multidimensional Arrays
2D-ARRAY
static void Main(string[] args)
{
int[,] a = new int[3, 4] {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
;}
for( int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
Console.WriteLine("a[{0},{1}]={2}", i, j, a[i, j]);
}
}
Console.ReadKey();
5
2D-ARRAY
6
When would you use a multi-dimensional array?
7
Declaring & initializing Multi-dimensional Arrays
string [,] names;
string[,] names = new string[5,4];
8
Initializing and accessing 3d array
static void Main(string[] args)
{
int[,,] arr = new int[3,2,2];
Console.WriteLine("Fill the array ");
for(int i=0; i<3;i++)
{
for(int j=0;j<2;j++ )
{
for (int k = 0; k < 2; k++)
{
arr[i, j, k] = Convert.ToInt32(Console.ReadLine());
}
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{
Console.WriteLine("the output at index [{0},{1},{2}]={3}", i, j, k, arr[i, j, k]);
}
}
}
Console.ReadKey();
}
9
OUTPUT
10
CLASS ACTIVITY
CREATE 4D ARRAY
INITIALIZE IT (by getting values from user at
runtime)
Display output
11
Lecture-4
JAGGED ARRAY
12
Jagged Arrays
13
Jagged array
A jagged array is an array whose elements are arrays,
possibly of different sizes. A jagged array is sometimes called
an "array of arrays.“
14
Syntax:
data_type[ ][ ] name_of_array = new data_type[rows][ ]
15
JAGGED ARRAY
The following is a declaration of a single-dimensional array that has three
elements, each of which is a single-dimensional array of integers:
Before you can use jaggedArray, its elements must be initialized. You can initialize
the elements like this:
16
Initializing
jaggedArray[0] = new int[ ] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[ ] { 0, 2, 4, 6 };
jaggedArray[2] = new int[ ] { 11, 22 };
17
JAGGED ARRAY
We can also initialize the array upon declaration
like this:
int[ ][ ] jaggedArray2 = new int[ ][ ]
{
new int[ ] { 1, 3, 5, 7, 9 },
new int[ ] { 0, 2, 4, 6 },
new int[ ] { 11, 22 }
};
18
JAGGED ARRAY
we can also use the following shorthand form. Notice that we
cannot omit the new operator from the elements initialization
because there is no default initialization for the elements:
int[ ][ ] jaggedArray3 =
{
new int[ ] { 1, 3, 5, 7, 9 },
new int[ ] { 0, 2, 4, 6 },
new int[ ] { 11, 22 }
};
19
JAGGED ARRAY
// Assign 77 to the second element ([1]) of the first array ([0]):
jaggedArray3[0][1] = 77;
20
2D MULTI-DIMENSIONAL ARRAY TO DATAGRID VIEW
What is Datagrid view in Windows form application ?
21
2D MULTI-DIMENSIONAL ARRAY TO DATAGRID VIEW
The .GetLength(0) method returns number of elements in
the row direction in a multidimensional array.
22
23
24
25