Array And Array List




Array And Array List


We use variable to save a value,when we have multiple values to be saved we can use Arrays.For Example, when we need to save 50 students names.one solution is using 50 variables to save 50 data.But, it is an tedious work.Instead of that we can use a single array to save 50 names.Thus the universal need of array is obvious.Names or values are called elements of array and no. of elements are called length of array.

An Array is a data structure that contains a number of variables that are accessed through computed indices.

We have two different categories of array:
Static Array and Dynamic Array



Static Array:

They are created during “design time”(before run-time).Advantage of this array is efficiency.

string []Names={"Arpit","Beil","Cirus"}



Dynamic Array:

They are created during run time.The example for this is given below



INSERTING VALUES FROM ARRAY TO A DATA TABLE

 for (int i = 0; i < number_of_rows; i++)
            {
                for (int j = 0; j < number_of_Columns; j++)
                {
                    int Count = int2Array[i, j];
                    dtTest.Rows[i][j] = int2Array[i, j];
                }

            }

Actual Array instances are created dynamically at runtime using the new operator. The new operation specifies the length of the new array instance, which is then fixed for the lifetime of the instance

This is an one dimensional Array.

Code snippet for this Array is Given Below

    Insertion of integers into Array

To populate an array one can simply specify values for each element, as in the following code:

            int[] intArray = new int[15];
            int Arraytype = intArray.Rank;
[Gives the dimension of the Array:here it is One]
            intArray[0] = 3;
            intArray[1] = 6;

            intArray[2] = 9;


Indeed, arrays are sometimes called finite mappings.symbolically , this mappings can be shown as

array_name(index value) --->element
  //intArray[0] = 3;

One can also run together the array declaration with the assignment of values to elements using

    int[] i = new int[] {1,2};

or the even shorter version of this:


    int[] i = {1,2};

    Retrieval of integers From Array

            foreach (int i in intArray)
            {
                string str = (i.ToString());

            }
            Response.Write("FirstArray:" + intArray[0] + "<br>" + "SecondArray:" + intArray[1] + "<br>" + "ThirdArray:" + intArray[2]);

C# also supports multi-dimensional arrays. The number of dimensions of an array type, also known as the rank of the array type, is one plus the number of commas written between the square brackets of the array type. The following example allocates a one-dimensional, a two-dimensional, and a three-dimensional array.

int[] one = new int[1];
int[,] two = new int[1, 2];
int[,,] three= new int[1, 2, 3];


This is an two dimensional Array.

Inserting values to a 2 dimensional Array

          int[,] int2Array = new int[3, 3];
            int2Array[0, 0] = 1;
            int2Array[0, 1] = 2;
            int2Array[0, 2] = 3;
            int2Array[1, 0] = 4;
            int2Array[1, 1] = 5;
            int2Array[1, 2] = 6;
            int2Array[2, 0] = 7;
            int2Array[2, 1] = 8;
            int2Array[2, 2] = 9;

To GET THE NUMBER OF ROWS AND COLUMNS


            int number_of_rows = int2Array.GetLength(0);
            int number_of_Columns = int2Array.GetLength(1);

ArrayList

ArrayList list = new ArrayList();
            list.Add(new Author("ABC", 30,));
            list.Add(new Author("XYZ", 35));
        

 public Author(string Name, Int16 Age)
        {
            this.Name = Name;
            this.Age = Age;
          
        }
 public string Name
        {
            get { return name; }
            set { name = value; }
        }
private Int16 age;

        public Int16 Age
        {
            get { return age; }
            set { age = value; }
        }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: