Arrays

Arrays are efficient containers for groups of types that can be accessed by index or iterated through. C# has three types of arrays: single dimension, multi-dimension, and jagged.

Single-Dimension Arrays

A single-dimension array is simply a single column of values of a given type. Listing 2.14 shows how to create and use a single-dimension array.

Listing 2.14 A Single-Dimension Array (SingleDimension.cs)
 using System; class SingleDimension {    static void Main()    {       int[] aInts = new int[2];       aInts[0] = 87;       aInts[1] = 15;       string[] aStrings = new string[] { "one", "two" };       char[] aChars = { 'a', 'b', 'c' };       foreach (char aChar in aChars)       {          Console.Write(aChar + ", ");       }       Console.ReadLine();    } } 

Arrays can be instantiated in three ways, as shown in Listing 2.14. The aInts array shows how to create a fixed-size array and set its values explicitly. The aStrings array shows shortcut notation when the array items are known. The aChars declaration is even more of a shorthand notation. Being able to express arrays in different ways offers a convenient choice that will depend on the circumstances under which the array was created.

Multi-Dimension Arrays

Multi-dimension arrays hold data of a given type in multiple dimensions. Listing 2.15 shows how to create and use a multi-dimension array.

Listing 2.15 A Multi-Dimension Array (MultiDimension.cs)
 using System; class MultiDimension {    static void Main()    {       int[,] aInts = new int[2,2];       aInts[0,0] = 33;       aInts[0,1] = 27;       aInts[1,0] = 61;       aInts[1,1] = 95;       for (int i=0; i <= aInts.GetUpperBound(0); i++)       {          for (int j=0; j <= aInts.GetUpperBound(1); j++)          {             Console.WriteLine("aInts[{0},{1}] = {2}", i, j, aInts[i,j]);          }       }       Console.ReadLine();    } } 

One of the important parts of Listing 2.15 is in the for loops, where the upper bound of each dimension is discovered. The GetUpperBound method takes a zero-based integer input parameter representing the dimension and returns the zero-based index of the upper bound of that dimension. In the case of Listing 2.15, both aInts.GetUpperBound(0) (the first dimension) and aInts.GetUpperBound(1) (the second dimension) return 1.

Jagged Arrays

Jagged arrays are arrays that have single-dimension arrays within another single-dimension array. Jagged arrays have two primary benefits over single or multi-dimension arrays. The first is that memory space can be optimized for information stored in lookup tables. Consider an array of the days within a month. Each month has a different number of days, so with a jagged array, only the number of spaces in memory for each individual month is allocated. Had this been a multi-dimension array, memory for 12x31 types would have been allocated. However, with a jagged array, memory is saved for a range from 28 to 31 days to accommodate all the variations, including February, which only has 28 days (29 in a leap year). Another is that when passing a variable as a params type parameter (discussed in the "ref and value Parameters" section of this chapter) to a method, only a single-dimension or jagged array may be passed, but not a multi-dimension array. Listing 2.16 shows how to create and use a jagged array.

Listing 2.16 A Jagged Array (Jagged.cs)
 using System; class Jagged {    static void Main()    {       int[][] aInts = new int[3][];       aInts[0] = new int[3];       aInts[1] = new int[2];       aInts[2] = new int[1];       aInts[0][0] = 1;       aInts[0][1] = 2;       aInts[0][2] = 3;       aInts[1][0] = 2;       aInts[1][1] = 2;       aInts[2][0] = 3;       for (int i=0; i < aInts.Length; i++)       {          for (int j=0; j < aInts[i].Length; j++)          {             Console.Write(" {0}", aInts[i][j]);          }          Console.WriteLine();       }       Console.ReadLine();    } } 

Listing 2.16 creates a jagged array with differing sizes on the second dimension. Each dimension can be the same, but they don't have to be. With jagged arrays, unlike multi-dimension arrays, you don't have to use GetUpperBound(n) to get the size of the n dimension. Because each dimension of the array is an array itself, you can get the Length property of any dimension directly.



C# Builder KickStart
C# Builder KickStart
ISBN: 672325896
EAN: N/A
Year: 2003
Pages: 165

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net