Advertisement

Responsive Advertisement

IV.3.Multidimensional Array, IV.4. Jagged Array, IV.5. Collection

 


IV.3.Multidimensional Array

A multi-dimensional array can be termed as an array of arrays that stores homogeneous data in tabular form. Data in multidimensional arrays are stored in row-major order.

The general form of declaring N-dimensional arrays is:  


data_type array_name[size1][size2]....[sizeN];

data_type: Type of data to be stored in the array.

array_name: Name of the array

size1, size2,… ,sizeN: Sizes of the dimension

Examples: 

Two dimensional array: int two_d[10][20];

Three dimensional array: int three_d[10][20][30]; 


Size of Multidimensional Arrays:

The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. 

For example: 

The array int x[10][20] can store total (10*20) = 200 elements. 

Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements.



IV.4. Jagged Array

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. Here, the number of rows will be fixed at the declaration time, but you can vary the number of columns.


Declaration

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.


Syntax:

data_type[][] name_of_array = new data_type[rows][]

Example:

int[][] jagged_arr = new int[4][]

In the above example, a single-dimensional array is declared that has 4 elements(rows), each of which is a 1-D array of integers.


Initialization

The elements of Jagged Array must be initialized before its use. You can separately initialize each array element. There are many ways to initialize the Jagged array’s element.


IV.5. Collection
For many applications, you want to create and manage groups of related objects. There are two ways to group objects: by creating arrays of objects, and by creating collections of objects.

Arrays are most useful for creating and working with a fixed number of strongly typed objects. For information about arrays, see Arrays.

Collections provide a more flexible way to work with groups of objects. Unlike arrays, the group of objects you work with can grow and shrink dynamically as the needs of the application change. For some collections, you can assign a key to any object that you put into the collection so that you can quickly retrieve the object by using the key.

A collection is a class, so you must declare an instance of the class before you can add elements to that collection.

If your collection contains elements of only one data type, you can use one of the classes in the System.Collections.Generic namespace. A generic collection enforces type safety so that no other data type can be added to it. When you retrieve an element from a generic collection, you do not have to determine its data type or convert it.









Post a Comment

0 Comments