Arrays

13
Arrays

description

C

Transcript of Arrays

  • Arrays

  • Collection of items of same DatatypeTypes of ArraysSimple ArraysMulti Dimensional ArraysJagged Arrays

  • Simple ArraysArray DeclarationAn array is declared by defining the type of the elements inside the array followed by empty brackets and a variable name E.g.: int[] myArray; Array InitializationAfter declaring an array, memory must be allocated to hold all the elements of the array. An array is a reference type, so memory on the heap must be allocated. This is done by initializing the variable of the array using the new operator with the type and the number of elements inside the array. Here you specify the size of the array.E.g. myArray = new int[4];Instead of using a separate line for the declaration and initialization, you can declare and initialize an array in a single line

  • Array Initialization (Contd.)int[] myArray = new int[4]; (OR)int[] myArray = new int[4] {4, 7, 11, 2}; (OR)int[] myArray = new int[] {4, 7, 11, 2}; (OR)int[] myArray = {4, 7, 11, 2}; Accessing Array Elements After an array is declared and initialized, you can access the array elements using an indexer. Arrays only support indexers that have integer parameters. The indexer always starts with a value of 0 for the first element. The highest number you can pass to the indexer is the number of elements minus one, since the index starts at zero. int[] myArray = new int[] {4, 7, 11, 2};int v1 = myArray[0]; // read first element int v2 = myArray[1]; // read second element myArray[3] = 44; // change fourth element

  • Print the elements of the arrayLength Property returns the no. of elements of the array

    for (int i = 0; i < myArray.Length; i++) { Console.WriteLine(myArray[i]); } (OR)foreach (int val in myArray) {Console.WriteLine(val); }

  • Multidimensional ArraysOrdinary arrays (also known as 1-dimensional arrays) are indexed by a single integer. A multidimensional array is indexed by two or more integers.The following matrix shows the mathematical notation for a 2-dimensional array that has three rows and three columns. The first row has the values 1, 2 and 3, whereas the third row has the values 7, 8 and 9. 1 2 3 4 5 6 7 8 9Declaration & Initialization of Two Dimensional Arrayint[,] twodim = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; Note: When using above array initializer, you must initialize every element of the array. It is not possible to leave the initialization for some values.

  • Declaration & Initialization of Three Dimensional Arrayint[,,] threedim = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } }, { { 9, 10 }, { 11, 12 } } }; Printing the elements of Multi-Dimensional Arrays // Loop to iterate through rowsfor ( int i=0 ; i < twodim.GetLength(0) ; i ++){// Loop to iterate throught columnsfor ( int j=0 ; j < twodim.GetLength(1) ; j++) {Console.Write(twodim[i,j] + );}Console.WriteLine();}

  • Jagged ArraysJagged Array is the kind of array which has array as its elementsTwo Dimensional Array Jagged Array

    1 2 3 1 2 4 5 6 3 4 5 6 7 7 8 9 8 9 10

    A jagged array is more flexible in sizing the array. With a jagged array every row can have a different size. Declaration & Initialization of Jagged ArrayA jagged array is declared by placing opening and closing brackets after another. With the initialization of the jagged array, first only the size that defines the number of rows is set. The second brackets that define the number of elements inside the row are kept empty as every row has a different number of elements

  • 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 };

    Printing the elements of Jagged Arrayfor (int row = 0; row < jagged.Length; row++){for (int element = 0; element < jagged[row].Length;element++){Console.Write(jagged[row][element] + ); }Console.WriteLine();}

  • Array ClassDeclaring an array with brackets is a C# notation of using the Array class. Using the C# syntax behind the scenes creates a new class that derives from the abstract base class Array. Properties

  • Creating Arrays The Array class is abstract, so you cannot create an array by using a constructor. However, instead of using the C# syntax to create array instances, it is also possible to create arrays by using the static CreateInstance() method. This is extremely useful if you dont know the type of the elements in advance, as the type can be passed to the CreateInstance() method as a Type object.The following example shows how to create an array of type int with a size of 5. Array intArray1 = Array.CreateInstance(typeof(int), 5); for (int i = 0; i < 5; i++) { intArray1.SetValue(33, i); }

  • SortingThe Array class implements a bubble-sort for sorting the elements in the array. The Sort() method requires the interface IComparable to be implemented by the elements in the array.string[] names = { "Christina", "Shakira", "Beyonce", "Gwen}; Array.Sort(names); ReverseReverse Method is used to reverse the elements of the array string[] names = { "Christina", "Shakira", "Beyonce", Gwen}; Array.Reverse(names); Copying ArraysThe Clone() method that is defined with this interface creates a shallow copy of the array. int[] intArray2 = (int[])intArray1.Clone();

  • THANK YOU