Jagged Array Example


Here is an example of how a Jagged Array works. The code I have included here also contains the decleration and it's operation.

using System;
public class MyArrayc2
{
public static void Main()
{
int [][]arr=new int[4][];
arr[0]=new int[3];
arr[1]=new int[2];
arr[2]=new int[5];
arr[3]=new int[4];

Console.WriteLine("Enter the numbers for Jagged Array");

for(int i=0 ; i < arr.Length ; i++)
{
for(int x=0 ; x < arr[i].Length ; x++)
{
String st= Console.ReadLine();
int num=Int32.Parse(st);
arr[i][x]=num;
}
}

Console.WriteLine("");
Console.WriteLine("Printing the Elemnts");

for(int x=0 ; x < arr.Length ; x++)
{
for(int y=0 ; y < arr[x].Length ; y++)
{
Console.Write(arr[x][y]);
Console.Write("\0");
}
Console.WriteLine("");
}
}
}

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!