Creation of a jagged two-dimensional array in C#

using System;
 
class Jagged {
  public static void Main() {
    int[][] jagged = new int[3][];
    jagged[0] = new int[4];
    jagged[1] = new int[3];
    jagged[2] = new int[5];
 
    int i;
 
    // store values in first array
    for(i=0; i < 4; i++)
      jagged[0][i] = i;
 
    // store values in second array
    for(i=0; i < 3; i++)
      jagged[1][i] = i;
 
    // store values in third array
    for(i=0; i < 5; i++)
      jagged[2][i] = i;
 
    // display values in first array
    for(i=0; i < 4; i++)
      Console.Write(jagged[0][i] + " ");
 
    Console.WriteLine();
 
    // display values in second array
    for(i=0; i < 3; i++)
      Console.Write(jagged[1][i] + " ");
 
    Console.WriteLine();
 
    // display values in third array
    for(i=0; i < 5; i++)
      Console.Write(jagged[2][i] + " ");
 
    Console.WriteLine();
 
  }
}

The output is shown here:

0 1 2 3
0 1 2
0 1 2 3 4

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.