Single-Dimensional Arrays
The type of each array declared is given firstly by the type of basic elements it can hold, and secondly by the number of dimensions it has. Single-dimensional arrays have a single dimension (ie, are of rank 1). They are declared using square brackets, eg: int[] i = new int[100]; This line of code declares variable i to be an integer array of size 100. It contains space for 100 integer elements, ranging from i[0] to i[99]. To populate an array one can simply specify values for each element, as in the following code: int[] i = new int[2]; 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}; By default, as we have seen, all arrays start with their lower bound as 0 (and we would recommend that you stick with this default). However, using the .NET framework's System.Array class it is possible to create and manipulate arrays with an alternative initial lower bound. The (read-only) Length property of an array holds the total number of its elements across all of its dimensions. As single-dimensional arrays have just one dimension, this property will hold the length of the single dimension. For instance, given the definition of array i above, i.Length is 2.
Rectangular Arrays
C# supports two types of multidimensional arrays: rectangular and jagged. A rectangular array is a single array with more than one dimension, with the dimensions' sizes fixed in the array's declaration. The following code creates a 2 by 3 multi-dimensional array: int[,] squareArray = new int[2,3]; As with single-dimensional arrays, rectangular arrays can be filled at the time they are declared. For instance, the code int[,] squareArray = {{1, 2, 3}, {4, 5, 6}}; creates a 2 by 3 array with the given values. It is, of course, important that the given values do fill out exactly a rectangular array. The System.Array class includes a number of methods for determining the size and bounds of arrays. These include the methods GetUpperBound(int i) and GetLowerBound(int i), which return, respectively, the upper and lower subscripts of dimension i of the array (note that i is zero based, so the first array is actually array 0). For instance, since the length of the second dimension of squareArray is 3, the expression squareArray.GetLowerBound(1) returns 0, and the expression squareArray.GetUpperBound(1) returns 2. System.Array also includes the method GetLength(int i), which returns the number of elements in the ith dimension (again, zero based). The following piece of code loops through squareArray and writes out the value of its elements (loops are covered in lesson 9).
A foreach loop can also be used to access each of the elements of an array in turn, but using this construction one doesn't have the same control over the order in which the elements are accessed.
Jagged Arrays
Using jagged arrays, one can create multidimensional arrays with irregular dimensions. This flexibility derives from the fact that multidimensional arrays are implemented as arrays of arrays. The following piece of code demonstrates how one might declare an array made up of a group of 4 and a group of 6 elements: int[][] jag = new int[2][]; The code reveals that each of jag[0] and jag[1] holds a reference to a single-dimensional int array. To illustrate how one accesses the integer elements: the term jag[0][1] provides access to the second element of the first group. To initialise a jagged array whilst assigning values to its elements, one can use code like the following: int[][] jag = new int[][] {new int[] {1, 2, 3, 4}, new int[] {5, 6, 7, 8, 9, 10}}; Be careful using methods like GetLowerBound, GetUpperBound, GetLength, etc. with jagged arrays. Since jagged arrays are constructed out of single-dimensional arrays, they shouldn't be treated as having multiple dimensions in the same way that rectangular arrays do. To loop through all the elements of a jagged array one can use code like the following:
or
|
Arrays
最新推荐文章于 2024-11-25 23:29:07 发布