11.4. Jagged Arrays

The multidimensional arrays that we have discussed are purely rectangular, meaning that all rows and columns have a uniform length. In jagged arrays, the array elements are themselves arrays of various dimensions and sizes (see Figure 11.1). A jagged array is also defined as an “array of arrays.”

Figure 11.1. A Jagged Array


Let's look at initialization of jagged arrays in C# and Java. There is no difference in the syntax of initializing jagged arrays in the two languages. A jagged array can be declared as an array of arrays, with each row having a different number of elements.

The following code snippet shows array declaration and initialization in C# and Java:

int [][] arr = new int[3][];

With the array declared, let's initialize each row in the array:

arr[0] = new int[] {1,2,3};
arr[1] = new int[] {1,2,3,4};
arr[2] = new int[] {1,2,3,4,5};

The size of each row is determined at compile time based on the number of elements in the {} brackets.

To create a jagged array, fire up the .NET IDE and copy into it the C# version of the jagged array code. The outer loop in Listing 11.5 loops through all the rows in the array. The number of rows is determined by the arr.length property (in C#) or the arr.Length property (in Java). This might sound strange for Java programmers because, for rectangular arrays, arr.length determines the total number of elements in the array. The inner loop loops through all the columns. The column length in each row is determined by arr[i].Length (in C#) or the arr[i].length (in Java).

Listing 11.5. JaggedArray.: Sample C# Jagged Array
using System;
class JaggedArray
{
  public static void Main()
  {
    int [][] arr = new int[3][];

    arr[0] = new int[] {1,2,3};
    arr[1] = new int[] {1,2,3,4};
    arr[2] = new int[] {1,2,3,4,5};

    for (int i = 0; i < arr.Length; i++)
    {
      System.Console.Write(";bsn");
      for (int j = 0; j < arr[i].Length; j++)
        System.Console.Write("arr[" + i + "," +
                       j + "]=" + arr[i][j] + " ");
    }
  }
}

The output of Listing 11.5 is as follows:

arr[0,0]=1 arr[0,1]=2 arr[0,2]=3
arr[1,0]=1 arr[1,1]=2 arr[1,2]=3 arr[1,3]=4
arr[2,0]=1 arr[2,1]=2 arr[2,2]=3 arr[2,3]=4 arr[2,4]=5

Listing 11.6 shows a sample jagged array in Java.

Listing 11.6. JaggedArray.Java: Sample Java Jagged Array
class JaggedArrays
{
  public static void main(String[] args)
  {
    int [][] arr = new int[3][];

    arr[0] = new int[] {1,2,3};
    arr[1] = new int[] {1,2,3,4};
    arr[2] = new int[] {1,2,3,4,5};

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

Next, we look at some of the utility methods of the System.Array class.

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

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