Adv Oop Lect10

Embed Size (px)

Citation preview

  • 8/13/2019 Adv Oop Lect10

    1/4

    Osman Khalid, Lecturer, Computer Sciences Department, COMSATS Institute of Information Technology

    Lecture (30 Oct)Ar ray Declarat ionint[] myArray;

    Ar ray Ini tiali zationmyArray = new int[4];

    int[] myArray = new int[4];

    You can also assign values to every array element using an array initializer. Arrayinitializers can be used only while declaring an array variable, not after the array isdeclared.

    int[] myArray = new int[4] {4, 7, 11, 2};

    If you initialize the array using curly brackets, the size of the array can also be left out,because the compiler can count the number of elements itself:

    int[] myArray = new int[] {4, 7, 11, 2};

    There s even a shorter form using the C# compiler. Using curly brackets you can writethe array declaration and initialization. The code generated from the compiler is thesame as in the previous example.

    int[] myArray = {4, 7, 11, 2};

    public class Person{public Person(){}

    public Person(string firstName, string lastName){this.FirstName = firstName;this.LastName = lastName;}public string FirstName { get; set; }public string LastName { get; set; }public override string ToString(){return String.Format({0} {1},FirstName, LastName);}}

    Person[] myPersons = new Person[2];

    myPersons[0] = new Person(Ayrton, Senna);myPersons[1] = new Person(Michael, Schumacher);

    Multidimensional Arraysint[,] twodim = new int[3, 3];twodim[0, 0] = 1;twodim[0, 1] = 2;

    Downloaded from: www.onspot.pk

  • 8/13/2019 Adv Oop Lect10

    2/4

    Osman Khalid, Lecturer, Computer Sciences Department, COMSATS Institute of Information Technology

    twodim[0, 2] = 3;twodim[1, 0] = 4;twodim[1, 1] = 5;twodim[1, 2] = 6;twodim[2, 0] = 7;twodim[2, 1] = 8;twodim[2, 2] = 9;

    int[,] twodim = {{1, 2, 3},{4, 5, 6},{7, 8, 9}};

    By using two commas inside the brackets, you can declare a 3 - dimensional array:

    int[,,] threedim = {{ { 1, 2 }, { 3, 4 } },{ { 5, 6 }, { 7, 8 } },{ { 9, 10 }, { 11, 12 } }};Console.WriteLine(threedim[0, 1, 1]);

    Jagged Arrays

    A jagged array is declared by placing one pair of opening and closing brackets afteranother. With the initialization of the jagged array, only the size that defines the numberof rows in the first pair of brackets is set. The second brackets that define the number ofelements inside the row are kept empty because every row has a different number ofelements. Next, the element number of the rows can be set for every row:

    int[][] jagged = new int[3][];jagged[0] = new int[2] { 1, 2 };jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 };jagged[2] = new int[3] { 9, 10, 11 };

    Iterating through all elements of a jagged array can be done with nested for loops. In the

    outer for loop every row is iterated, and the inner for loop iterates through every elementinside a row.

    for (int row = 0; row < jagged.Length; row++){for (int element = 0;element < jagged[row].Length; element++){Console.WriteLine(

    Downloaded from: www.onspot.pk

  • 8/13/2019 Adv Oop Lect10

    3/4

    Osman Khalid, Lecturer, Computer Sciences Department, COMSATS Institute of Information Technology

    row: {0}, element: {1}, value: {2},row, element, jagged[row][element]);}}The outcome of the iteration displays the rows and every element within the rows:row: 0, element: 0, value: 1row: 0, element: 1, value: 2row: 1, element: 0, value: 3row: 1, element: 1, value: 4row: 1, element: 2, value: 5row: 1, element: 3, value: 6row: 1, element: 4, value: 7row: 1, element: 5, value: 8row: 2, element: 1, value: 9row: 2, element: 2, value: 10row: 2, element: 3, value: 11

    Creating ArraysThe Array class is abstract, so you cannot create an array by using a constructor.

    Array intArray1 = Array.CreateInstance(typeof(int), 5);

    for (int i = 0; i < 5; i++){intArray1.SetValue(33, i);}for (int i = 0; i < 5; i++){Console.WriteLine(intArray1.GetValue(i));}

    You can also cast the created array to an array declared as int[] :

    int[] intArray2 = (int[])intArray1;

    The CreateInstance() method has many overloads to create multidimensional arrays andalso to create arrays that are not 0 - based. The following example creates a 2 -dimensional array with 2 3 elements. The first dimension is 1 - based; the seconddimension is 10 - based.

    int[] lengths = { 2, 3 };int[] lowerBounds = { 1, 10 };

    Array racers = Array.CreateInstance(typeof(Person), lengths, lowerBounds);

    Setting the elements of the array, the SetValue() method accepts indices for every dimension:

    racers.SetValue(new Person(Alain, Prost), 1, 10);racers.SetValue(new Person(Emerson, Fittipaldi), 1, 11);racers.SetValue(new Person(Ayrton, Senna), 1, 12);racers.SetValue(new Person(Ralf, Schumacher), 2, 10);racers.SetValue(new Person(Fernando, Alonso), 2, 11);racers.SetValue(new Person(Jenson, Button), 2, 12);

    Downloaded from: www.onspot.pk

  • 8/13/2019 Adv Oop Lect10

    4/4

    Osman Khalid, Lecturer, Computer Sciences Department, COMSATS Institute of Information Technology

    Although the array is not 0 - based you can assign it to a variable with the normal C#notation. You just have to pay attention to not crossing the boundaries.

    Person[,] racers2 = (Person[,])racers;Person first = racers2[1, 10];Person last = racers2[2, 12];

    SortingThe Array class implements a bubble - sort for sorting the elements in the array. TheSort() method requires the interface IComparable to be implemented by the elements inthe array. Simple types such as System.String and System.Int32 implementIComparable , so you can sort elements containing these types.With the sample program, the array name contains elements of type string, and thisarray can be sorted:string[] names = {Christina Aguilera,Shakira,Beyonce,Gwen Stefani

    };Array.Sort(names);foreach (string name in names){Console.WriteLine(name);}The output of the application shows the sorted result of the array:BeyonceChristina AguileraGwen StefaniShakira

    Downloaded from: www.onspot.pk