11.3. Multidimensional Arrays

Arrays can have more than one dimension. The rank of an array denotes the array's dimension. Two-dimensional arrays are indexed with two parameters and can be visualized as elements stored in a table with rows and columns. Elements can be accessed by specifying row and column numbers. Three-dimensional arrays can be thought of as layers of two-dimensional arrays. Basically, three-dimensional arrays require three parameters to index an element.

Let's look at some of the similarities and differences in initializing and declaring multidimensional arrays in C# and Java.

Surprisingly, C# has its own way of declaring multidimensional arrays. In C#, individual dimensions are separated by commas within [] brackets; in contrast, in Java individual dimensions are based on the [] brackets: You use [][] for two-dimensional arrays, and [][][] for three-dimensional arrays.

Here is a two-dimensional array declaration in C#:

int[,] arr = new int[5,2]
int arr[,] = new [5,2] // ERROR, won't work

Here are two-dimensional array declarations in Java:

int[][] arr = new int[5][2]
int arr[][] = new int[5][2]

Arrays can be initialized and declared at the same time. The following examples show initialization and declaration of two-dimensional arrays in C# and Java.

Two-dimensional array declaration and initialization in C#:

int[,] b = {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}};

Two-dimensional array declaration and initialization in Java:

int[][] b = new int[][] {{10, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}};
int[][] b = {{10, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}};

Now let's dissect a simple two-dimensional array as shown in Listing 11.3 and then compare it to the Java version shown in Listing 11.4. First, we declare and initialize the two-dimensional array.

int[,] b = {{1, 6}, {2, 7}, {3, 8}, {4, 9}, {5, 10}};

We have two for loops: one for rows, and a second one for columns. Dimension 0 indicates rows, and dimension 1 indicates columns. The length of an array of any dimension in C# is obtained by the GetLength(x) method, where x indicates the dimension. Java and C# store the elements in a similar manner except for a few syntactical differences. In C#, b.Length gives the total number of elements in an array, whereas in Java b.length gives the number of rows in the array.

Listing 11.3. BasicTwoDimensionalArrayClass.CS: A Two-Dimensional Array Sample in C#
using System;
class BasicTwoDimensionalArrayClass
{
  public static void Main()
  {
    int[,] b = {{1, 6}, {2, 7}, {3, 8}, {4, 9}, {5, 10}};

    for (int i=0; i < b.GetLength(0); i++ )
    {
      Console.Write(";bsn");
      for (int j = 0; j < b.GetLength(1); j++)
        Console.Write("b[" + i + "," + j + "]=" + b[i,j] + " ");
    }
  }
}

The output of Listing 11.3 is as follows:

b[0,0]=1 b[0,1]=6
b[1,0]=2 b[1,1]=7
b[2,0]=3 b[2,1]=8
b[3,0]=4 b[3,1]=9
b[4,0]=5 b[4,1]=10

Listing 11.4. BasicTwoDimensionalArray.Java: A Two-Dimensional Array Sample in Java
class BasicTwoDimensionalArray
{
  public static void main(String[] args)
  {
    int[][] b = {{1, 6}, {2, 7}, {3, 8}, {4, 9}, {5, 10}};

    for (int i=0; i < b.length; i++ )
    {
      System.out.print(";bsn");
        for (int j = 0; j < b[i].length; j++)
        System.out.print("b[" + i + "," + j + "]=" + b[i][j] + " ");
    }
  }
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.144.197