Advertisement

Handling Arrays

By Bintu Chaudhary   Posted at  5:20:00 AM   Two Dimensional Arrary

Home  »  C#  »  Handling Arrays


     When there is a need to use many variables then we There is a big problem because we will Conflict with name of variables So that in this Situation where we wants to Operate on many numbers then we can use array .Arrays are Set of Elements having Same data type or We can Say that Arrays are Collection of Elements having same name and same data type But Always Remember Arrays are Always Start From its index value and the index of array is start From 0 to n-1. Suppose we wants to Access 5th Element of array then we will use 4th Element Because Arrays are Start From 0 and arrays are always stored in Continuous Memory Locations The Number of Elements and Types of array are Identified by Subscript of array Elements. The Various types of Array those are provided by c# are as Follows:-
Single Dimensional Array
Two Dimensional Array

//Array Initialization
using System;
class arr
{
  public static void Main()
{
    int []a= {25,35,24,64,35};     //shortcuts version
    int l;
    l = a.length
;     int i;

    for(i=0;i < l; i++)
    {
      Console.WriteLine(a[i]);
    }
  }
}


Contents



Single Dimensional Array

     These are Mostly used when we wants to declare number of Elements Single Dimensional arrays are used for Using Many elements and they are Declared with Single Bracket Like int [] a etc. the Bracket will specify the size of array Elements Means how many array elements are declared and used For Accessing or using a variable we have to Specify the Index Value in the Sub Script or in Bracket of Array Variable.
The [] is used for dimensional or the sub-script of the array that is generally used for declaring the elements of the array For Accessing the Element from the array we can use the Subscript of the Array like this.

a[3]=100;

So there is only the single bracket then it is called as Single Dimensional Array
The Syntax For Declaring Array Elements in C# is as Follows-

Int [] a =new int[5];

Note the new Keyword , This is a Special Operator Which is used for Creating new Memory which is Continuous in nature and The new Keyword is Also Creating For Memory For Number of elements That is Specified in Single bracket.
A user can either declare an Array either in a Single Line or either he may declare an Array in two ways like this

Int [] a;
a=new int [5];



Output


Two Dimensional Array

     The Two Dimensional array Elements are used when we Wants to Perform the Operation in Matrix Forms the Two Dimensional arrays are used For Creating Matrix and Displaying array elements in the Form of Rows and Columns The Total Elements of Arrays Will be Calculated by Multiplying the Elements of Rows and Columns Like int a[3][3] i. e 9 Elements will be used by Array These are Called as Two Dimensional Because they use two Brackets.

Like this int a[3][3];

So This is the Example of the Two Dimensional Array In this first 3 represents the total number of Rows and the Second Elements Represents the Total number of Columns The Total Number of elements are judge by Multiplying the Numbers of Rows * Number of Columns in The Array in the above array the Total Number of elements are 9.
The Two Dimensional arrays are Declared as Follows:-

Int [ , ] a =new int[3,3];
Or
Int [ , ] a;
a=new int [3,3];

C# also Provides us a Class which is called as System.Array which Contains various Method those have applied on to the Arrays Like:-
1) Length :- For Calculating the Length of Array Variable.
2) Clear:- For Vanishing all Elements of Array.
3) Sort :- For Arranging Array Elements in Ascending Order.
4) Reverse :- For Arranging Array Elements into Descending Order.
5) Getvalue :- For Retrieving a value from a Particular Index of Array.



Output





ArrayList Class

     System Namespace also Contains a ArrayList Class which works on collection of Values and This is used for dynamic Operations Like Insertion , Deletion and Arranging the Elements into the List of Arrays For Making a List of Array we have to Declare a ArrayList by using

ArrayList a=new ArrayList();

Here we are not Specifying the Number of Elements of a List then the Default Number of Arguments are 16 and if a user wish he can increase or decrease the size of Array Elements for a List The Various Methods those are Related with the ArrayList Class are :-
1) Add :- For Inserting an Element into a List.
2) Capacity :- Returns Capacity or Space For Newly Elements.
3) Count :- Returns total Number of Elements those are Present in the List.
4) Insert :- For Inserting a New Element into a List.
5) Remove :- For Removing an Element from a List.
6) Sort :- For Arranging Elements into a Specific Order.
7) RemoveAt :- Removing a Element From a Particular Index.
8) RemoveRange :- For Removing Number of Elements from a List.
9) Contains :- For Checking if a List Contains an Element.





Output





About the Author

Nulla sagittis convallis arcu. Sed sed nunc. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.
View all posts by: BT9

Back to top ↑
Connect with Me