C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Jagged Array in C#


Posted Date: 06 Nov 2008    Resource Type: Code Snippets    Category: C# Syntax
Author: Shanthi MMember Level: Gold    
Rating: 1 out of 5Points: 7



C# treats multidimensional( arrays as 'arrays of arrays'. It is possible to declare a two-dimensional(2D) array as follows:

int [][] x = new int [3][];
x[0] = new int [2];
x[1] = new int [4];
x[2] = new int [3];

These statements create a 2D array having different lengths for each row. These variable size arrays are called 'Jagged Arrays'.

Jagged Arrays:

The following code snippet shows how to implement jagged arrays in C#.


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

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

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

Console.WriteLine("Jagged Array Elemnts");

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


The difference in the way we access:

With rectangular arrays(2D arrays), all indices are within one set of square brackets.

With Jagged arrays(variable size arrays), each element is within its own square brackets.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Jagged Array in C#  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Multiple Main Methods in C#
Previous Resource: Difference between IS and AS keyword in C#?
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use