110 likes | 240 Views
CSC 298. Arrays in C#. 1D arrays. A collection of objects of the same type array of integers of size 10 int [] a = new int [10]; The size can be specified at run time. int n = ReadInt(); int [] a = new int [n]; An array is a reference type. It is defined on the heap
E N D
CSC 298 Arrays in C#
1D arrays • A collection of objects of the same type • array of integers of size 10 • int[] a = newint[10]; • The size can be specified at run time. • int n = ReadInt(); • int[] a = newint[n]; • An array is a reference type. It is defined on the heap • As in C++ and Java, the first element has index 0. • To iterate through an array (use the Length property) • for(int i=0; i<a.Length; i++) • { /* work with a[i] */ }
Rectangular arrays • Arrays with 2 or more dimensions • array of strings of size 10 by 20 (10 rows by 20 columns) • string[,] a = newstring[10,20]; • Useful properties and methods • Length: total number of elements • Rank: total number of dimensions • GetLength(int dimension): number of elements in the specified dimension • To iterate through a rectangular array • for(int i=0; i<a.GetLength(0); i++) • for(int j=0; j<a.GetLength(1); j++) • { /*Work with a[i,j]*/ }
Jagged arrays • In a 2D rectangular array all rows have the same number of columns. • If this is not desirable, use a jagged array • // an array to store the daily rainfall • // during March, April and May • double[][] a = newdouble[3][]; • a[0] = newdouble[31]; // March • a[1] = newdouble[30]; // April • a[2] = newdouble[31]; // May • // To iterate • for(int i=0; i<a.Length; i++) • for(int j=0; j<a[i].Length; j++) • { /* Work with a[i][j] */ } • Think of a as an array of arrays.
Array initialization • The array initializer must be on the line of declaration int[] a = new int[]{1,2,3}; //shorthand int[] a = {1,2,3}; //rectangular 3x2 array int[,] b = newint[3,2]{{1,2},{3,4},{5,6}}; //shorthand int[,] b = {{1,2},{3,4},{5,6}}; // jagged array int[][] c = new int[2][]{new int[]{1,2,3},new int[]{4,5}}; //shorthand (must use new for the inner //arrays) int[][] c = {new int[]{1,2,3},new int[]{4,5}};
foreach loop (1) • A convenient construct to iterate through an array • string[] a = new string[10]; • /* code to initialize a */ • ... • // Iterate through a • foreach(string s in a) • { /* work with s */ } • How would you iterate through a rectangular or a jagged array with this construct?
foreach loop (2) • Rectangular array (same as for 1D array) • string[,] a = new string[10,20]; • /* code to initialize a */ • ... • // Iterate through a • foreach(string s in a) • { /* work with s */ } • Jagged array • string[][] a = new string[2][]; • a[0] = new string[5]; a[1] = new string[3]; • /* code to initialize a */ • ... • // Iterate through a (=array of arrays) • foreach(string[] arr in a) • foreach(string s in arr) • { /* work with s */ }
Array class • A class that contains many useful methods to work on arrays • A sampling (see the documentation for more details) • // Reverse the order of the elements • // of a 1D array a • Array.Reverse(a); • // Find an object o in a 1D array a • // o could be an int, a string... • // Note: the array must be sorted • int index = Array.BinarySearch(a,o); • // Sort a 1D array • // Note: the elements of the array • // must be comparable. • Array.Sort(a);
ArrayList class (1) • To get an array whose size can change dynamically, use an ArrayList (in System.Collections) • An ArrayList is a dynamic array of objects. • object is the ultimate base class of all objects in C# (more on this soon). All struct and class instances are of type object. • Thus, you can put anything you want in an ArrayList.
ArrayList class (2) • Simple example • ArrayList a = new ArrayList(); • a.Add(4); // a is {4} • a.Add("Hello"); // a is {4,"Hello"} • Console.Write(a.Count); // 2 is printed • a.Remove(a[0]); // a is {"Hello"} • a.Add(Color.Red); //a is {"Hello",Color.Red} • string s = (string)a[0]; // s is "Hello" • a.Remove(Color.Red); // a is {"Hello"} • // could also do a.Remove(a[1]); • See the class web site for a more complete example
Iterating through an ArrayList • ArrayList a = new ArrayList(); • // Initialize a ... • // To iterate, can use a for loop • for(int i=0; i<a.Count; i++){ /* work with a[i] */ } • // or a foreach loop • foreach(object o in a){ /* work with o */ } • // Can also use an IEnumerator • IEnumerator ietr = a.GetEnumerator(); • while(ietr.MoveNext()) { • object o = ietr.Current; • /* work with o */ • }