Sei sulla pagina 1di 4

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

Lecture (30 Oct) Array Declaration


int[] myArray;

Array Initialization
myArray = new int[4]; int[] myArray = new int[4];

You can also assign values to every array element using an array initializer. Array initializers can be used only while declaring an array variable, not after the array is declared.
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 write the array declaration and initialization. The code generated from the compiler is the same 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 Arrays int[,] twodim = new int[3, 3]; twodim[0, 0] = 1; twodim[0, 1] = 2;

Downloaded from: www.onspot.pk

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 after another. With the initialization of the jagged array, only the size that defines the number of rows in the first pair of brackets is set. The second brackets that define the number of elements inside the row are kept empty because every row has a different number of elements. 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 element inside 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

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: 1 row: 0, element: 1, value: 2 row: 1, element: 0, value: 3 row: 1, element: 1, value: 4 row: 1, element: 2, value: 5 row: 1, element: 3, value: 6 row: 1, element: 4, value: 7 row: 1, element: 5, value: 8 row: 2, element: 1, value: 9 row: 2, element: 2, value: 10 row: 2, element: 3, value: 11

Creating Arrays The 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 and also 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 second dimension 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

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];

Sorting The 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. Simple types such as System.String and System.Int32 implement IComparable , so you can sort elements containing these types. With the sample program, the array name contains elements of type string, and this array 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: Beyonce Christina Aguilera Gwen Stefani Shakira

Downloaded from: www.onspot.pk

Potrebbero piacerti anche