.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Collections >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Jagged Array Example

Posted By:Amit Mehra       Posted Date: November 18, 2008    Points: 10    Category: Collections    URL: http://www.dotnetspark.com  
 

Jagged Array is arrays of array in .NET Framework. Below is the example to create Jagged Array in C#.

int [][] jaggedArray = new int [5][];
jaggedArray[0] = new int [5];
jaggedArray[1] = new int [6];
jaggedArray[2] = new int [7];
jaggedArray[3] = new int [8];
jaggedArray[4] = new int [9];

Above declaration creates 2 Dimensional array with different lengths for each row. These index shows the size arrays are and referenced as Jagged Arrays.

The below code shows example of Jagged array in C#.

using System;public class JaggedArrayExample

{

public static void Main()

{

// Declaration of Jagged Array

int [][] jaggedArray = new int [5][];

jaggedArray[0] = new int [5];

jaggedArray[1] = new int [6];

jaggedArray[2] = new int [7];

jaggedArray[3] = new int [8];

jaggedArray[4] = new int [9];

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

for(int i=0 ; i < jaggedArray.Length ; i++)

{

for(int j=0 ; j < jaggedArray[i].Length ; j++)

{

String arrStr= Console.ReadLine();

int n=Int32.Parse(arrStr);

jaggedArray[i][j]= n;

}

}

Console.WriteLine("Result of Jagged Array Elemnts are like below for the entered number");

for(int i=0 ; i < jaggedArray.Length ; i++)

{

for(int j=0 ; j < jaggedArray[i].Length ; j++)

{

Console.Write(jaggedArray[i][j]+" ");

}

Console.WriteLine("\t");

}

}

}

Hope this help to beginner to understand Jagged Array concept in C#.



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend