Saturday, April 4, 2015

what is Array, Types of Array in C#

what is Array, Types of Array in C#

An array is the collection of the variable of the same data type that is referred to by a common name.  
In C#,arrays can have one or more dimension.

The principle advantage of array is that it arranges data in such a way that it can be easily manipulated.

In c#,the arrays are implemented as objects.

One-Dimension Array

One dimension array is the list of variables. 
This list is common in programming.

First you must declare a variable that can be referred to an array.

Second you must create an instance of an array by use of new.

Syntax
Type[] arrayname=new type[size];

Example:

            int[] arr1 = new int[10];

            for (int i = 0; i < 10; i++)
            {
                Console.Write("enter value : ");
                arr1[i] = Convert.ToInt32(Console.ReadLine());
            }

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("value : " + arr1[i]);

            }
//-----------------------


Arrays can be initialized when they are created.

The general form for initializing one dimensional array is shown here:

Type[] arrayname={val1,val2…};


Multidimensional Array

A multidimensional array is an array that has two or more dimensions and an individual element is accessed through the combination of two or more indices.

Two-dimensional array—simple form of multidimensional array.
Location of element is specified by two indexes.

Think of two dimensional array as a table of information, one index indicates the row ,other indicates the column.

Int[,] table=new int[10,20].
Separated by comma.
To access an element in a two dimension array, you must specify both indices ,separating the two with a comma.
like.
Table[3,5]=10;

                  int[,] arr2 = new int[3, 3];

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.Write("Enter Value : ");
                    arr2[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.WriteLine("value : " + arr2[i, j]);
                }
            }


Jagged Array

A jagged array is an array of array in which length of each array can differ.

Thus a jagged array can be used to create a table in which the row length are not the same.

Jagged array are declared by using sets of square brackets to indicate each dimension.

Type[][] arrayname=new type[size][];

Size indicates number of rows in the array.

The rows themselves have not been allocated.

Instead the rows are allocated individually.

Example

            int[][] arr3 = new int[3][];
            arr3[0] = new int[2];
            arr3[1] = new int[3];
            arr3[2] = new int[4];

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < arr3[i].Length; j++)
                {
                    Console.Write("Enter value : ");
                    arr3[i][j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < arr3[i].Length; j++)
                {
                    Console.WriteLine("arr3[{0}][{1}] : {2} ", i, i, arr3[i][j]);
                }

            }


No comments:

Post a Comment