You are on page 1of 25

Lecture-3

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?

Multi-dimensional arrays are an extended form of


one-dimensional arrays and are frequently
used to store data for mathematic computations,
image processing, and record management.

7
Declaring & initializing Multi-dimensional Arrays
string [,] names;
string[,] names = new string[5,4];

int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };


int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };

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.“

Jagged array is a array of arrays such that member arrays can


be of different sizes. In other words, the length of each array
index can differ.

The elements of Jagged Array are reference types and


initialized to null by default. Jagged Array can also be mixed
with multidimensional arrays.

14
Syntax:
data_type[ ][ ] name_of_array = new data_type[rows][ ]

In Jagged arrays, user has to provide the number of rows


only. If the user is also going to provide the number of
columns, then this array will be no more Jagged Array.

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:

int[ ][ ] jaggedArray = new int[3][ ];

Before you can use jaggedArray, its elements must be initialized. You can initialize
the elements like this:

jaggedArray[0] = new int[5];


jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];
Each of the elements is a single-dimensional array of integers. The first element is an array of 5
integers, the second is an array of 4 integers, and the third is an array of 2 integers.

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;

// Assign 88 to the second element ([1]) of the third array ([2]):


jaggedArray3[2][1] = 88;

20
2D MULTI-DIMENSIONAL ARRAY TO DATAGRID VIEW
 What is Datagrid view in Windows form application ?

 The DataGridView control provides a powerful and flexible way


to display data in a tabular format.

 You can use the DataGridView control to show read-only views


of a small amount of data, or you can scale it to show editable
views of very large sets of data.

 This control also let's you display data in a master-details view.

21
2D MULTI-DIMENSIONAL ARRAY TO DATAGRID VIEW
 The .GetLength(0) method returns number of elements in
the row direction in a multidimensional array.

 For a 2x6 array that is 2.


 The .GetLength(1) method returns number of elements in
the column direction in a multidimensional array.

 For a 2x6 array that is 6.

22
23
24
25

You might also like