12.2 Multi-dimensional arrays: rectangular arrays


Rectangular arrays are, as the name implies, arrays which look like a table. A 2D rectangular array can be represented as a plane rectangle, and a 3D rectangular array can be represented as a cuboid. [4] Whatever the case, the idea is the same “ in rectangular arrays, every row has the same number of columns . Jagged arrays differ from this as you will see later.

[4] Some people have great difficulty understanding 4D arrays and above because it is difficult to picture a 4D rectangular array as represented by a simple object (a 4D shape?) in our 3D world.

The syntax for rectangular arrays is different from Java. Study the statements below and note the syntactical differences.

  • Declaring a 2D rectangular array (without instantiating it):

     In Java: int [][]MyArray1; In C#:  int [,]MyArray1;  
  • Declaring and instantiating a 2D rectangular array in one statement:

     In Java: int [][]MyArray1 = new int[2][5]; In C#:  int [,]MyArray1  =  new int[2,5];  

    The statement above declares MyArray1 as a 2D int array with 2 rows and 5 cells in each row. [5]

    [5] Like most people, I am visualizing the 2D array as a table. The first level array is the row, while the second level array is the number of cells in each row.

    Figure 12.2 shows what MyArray1 looks like (default values for each ' cell ' is 0).

    Figure 12.2. MyArray1 .

    graphics/12fig02.gif

  • Declaring, instantiating and initializing a 2D rectangular array in one statement:

     In Java: int [][]MyArray2 = {{1,2,3},{4,5,6}}; In C#:  int [,]MyArray2  =  {{1,2,3},{4,5,6}};  

    The statement above declares a 2D int array called MyArray2 . MyArray2 is a 2 x 3 array with 2 rows and 3 cells in each row. Figure 12.3 gives a visual representation of the array.

    Figure 12.3. MyArray2 .

    graphics/12fig03.gif

  • Accessing a value in a 2D rectangular array:

     In Java: int Temp = MyArray1[1][0]; In C#:  int Temp  =  MyArray1[1,0];  
  • Assigning a value to an array element:

     In Java: MyArray1[2][3] = 99; In C#:  MyArray1[2,3]  =  99;  

12.2.1 Using the Length property

I mentioned earlier that the Length (with a capital 'L') property of an array in C# gives the total size of an array. For a multi-dimensional array, it gives the total size of the whole array instead of just the size of the ' first-level array'.

For example, if you have a 2D rectangular array of 2 rows of 5 cells each created by this statement:

 int [,] MyArray = new int[2,5]; 

In Java, MyArray.length will return 2 . In C#, MyArray.Length will return 10 (2 x 5). To obtain the size of the first-level array (the number of rows), use MyArray.GetLength(0) (which returns 2 ). To obtain the size of the second-level array (the number of cells in each row), use MyArray.GetLength(1) (which returns 5 ).

Study the output of the following class to confirm your understanding:

 1: using System;  2:  3: public class TestClass{  4:   int [,]MyArray = new int[2,5]; // new 2D array  5:  6:   public static void Main(){  7:     TestClass c = new TestClass();  8:     Console.WriteLine(c.MyArray  .Length  );  9:     Console.WriteLine(c.MyArray  .GetLength(0)  ); 10:     Console.WriteLine(c.MyArray  .GetLength(1)  ); 11:   } 12: } 

Output:

 c:\expt>test 10 2 5 


From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

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