Creating Multi-Dimensional Arrays


C# also has a syntax for creating multidimensional arrays along with a mechanism for investigating how many dimensions an array has and the size of each dimension.

To create a two-dimensional array:

  1. Type the name of the type of the array, for example: int .

  2. Type an open square bracket [ .

  3. Type a comma , .

  4. Type a close square bracket ] .

  5. Type the name of the variable for the array, for example: nums .

  6. Type an equal sign = .

  7. Type new followed by the type of the array, for example: new int .

  8. Type an open square bracket [ .

  9. Type the size of each dimension separated by commas, for example: [4,5] .

  10. Type a close square bracket ] .

  11. Type a semicolon ; ( Figure 9.25 ).

    Figure 9.25 The number of commas in the declaration determines the number of dimensions for the array. So integer[,,,] would be a four-dimensional array of integers.
     class Cell {    public string Text; }  Cell[,] grid = new Cell[10,10];  void Task() {  grid[0,1] = new Cell();   grid[0,1].Text = "Address";  } 

graphics/tick.gif Tips

  • If you want to have more than two dimensions in the array, simply add more commas in the declaration of the array, for example: int[,,] nums = new int[2,1,4]; ( Figure 9.26 ). A company I visited once had a need for a 20-dimensional array (yikes!).

    Figure 9.26 And the dimensions keep growing. For homework tonight, see how many commas you can enter for dimensions before the compiler complains.
     double[,,] spaceCoords = new double[3,4,5]; 
  • You can also use the curly notation for initializing the array in place with multidimensional arrays ( Figure 9.27 ).

    Figure 9.27 It starts getting difficult to see what is happening in the initialization when you have multiple dimensions. In this case we're creating a twodimensional array of two rows by three columns .
     double[,] angles = new double[,]                   {  {-45.3,12.5,5}, {33,22,11}  }; 
  • The Length property returns the total number of elements in the array. Thus, the Length is equal to the product of all dimensions. System.Array provides a function called GetLength that returns the size of each dimension in the array. The property Rank in System.Array provides the number of dimensions for the array.

  • System.Array provides a version of the CreateInstance function that enables you to create multi-dimensional arrays in which the lower bounds are not zero ( Figure 9.28 ).

    Figure 9.28 For some reason, it isn't possible to create arrays with a single dimension that have negative lower bounds; they have to be multi-dimensional arrays. Also, arrays that have non-zero lower bounds may not be compatible with other languages.
     class Point {    public int x;    public int y; } void Task() {    Point[,] Graph = (Point[,])    Array.CreateInstance(       typeof(Point), //type of array       new int[] {10,10}, //sizes  new int[] { -5, -5}); //lower   //bounds  for (int  x = -5  ; x < 5; x++)    {       for (int  y = -5  ; y < 5; y++)       {          Graph[x,y] = new Point();       }    } } 
  • C# also uses the concept of jagged arrays. That's an array in which each element is also an array, and each sub-array can be of a different size. Instead of using a comma to specify dimensions, a jagged array is declared with two sets of square brackets ( Figure 9.29 ).

    Figure 9.29 Jagged arrays are arrays of arrays, got that? Basically every element in the array is a subarray . . . Oh, never mind!
     string[][] families = new string[2][]; families[0] = new string[5]; families[1] = new string[4]; families[0][0] = "Bill"; families[0][1] = "Carole"; families[0][2] = "Bradley"; families[0][3] = "Madalyn"; families[0][4] = "Duncan"; families[1][0] = "Jose"; families[1][1] = "Laurel"; families[1][2] = "Alex"; families[1][3] = "Andy"; 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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