IT Community - Software Programming, Web Development and Technical Support

Jagged Arrays in C#

This is a discussion on Jagged Arrays in C# within the C# Programming forums, part of the Software Development category; Hi Guys can you explain me What are Jagged Arrarys ?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 05-17-2007, 05:40 AM
vigneshgets vigneshgets is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 904
vigneshgets is on a distinguished road
Default Jagged Arrays in C#

Hi Guys can you explain me What are Jagged Arrarys ?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-19-2007, 05:07 AM
Gopisoft Gopisoft is offline
D-Web Sr.Programmer
 
Join Date: Feb 2007
Posts: 117
Gopisoft is on a distinguished road
Default Re: Jagged Arrarys

A Jagged array is an array whose elements are also an array.the Elements of Jagged array can be of different sizes.It is Called as Array of Arrays.It is Called jagged because of its row in different SIze.

Jagged array declaration in C#:

Code:
 

int [] [] myJaggedArray = new int [3][];

Declaration of inner arrays: 
 
 myJaggedArray[0] = new int[5] ;   // First inner array will be of length 5.
 myJaggedArray[1] = new int[4] ;  // Second inner array will be of length 4.
 myJaggedArray[2] = new int[3] ;   // Third inner array will be of length 3.

Now to access third element of second row we write:

  int value = myJaggedArray[1][2];
Note: Jagged arrays are not rectangular arrays Like Multi-Diemnsional Arrays.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-23-2007, 12:10 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: Jagged Arrarys

Jagged array means an array of arrays. Jagged arrays can be declared and instantiated in a single statement—using side-by-side square braces—but there is no way to initialize the elements of the jagged array in the same statement.

Here is the example for Jagged Array in C#.

Code:
int[][] jagged = new int[3][];
            jagged[0] = new int[2];
            jagged[1] = new int[3];
            jagged[2] = new int[2];

            int i = 0;
            for (i = 0; i < 2; i++)
                jagged[0][i] = i;
            for (i = 0; i < 3; i++)
                jagged[1][i] = i;
            for (i = 0; i < 2; i++)
                jagged[2][i] = i;

            Console.WriteLine("displaying first array");
            for (i = 0; i < 2; i++)
                Console.Write(jagged[0][i] + " ");

            Console.WriteLine();

            Console.WriteLine("displaying second array");
            for (i = 0; i < 3; i++)
                Console.Write(jagged[1][i] + " ");

            Console.WriteLine();

            Console.WriteLine("displaying third array");
            for (i = 0; i < 2; i++)
                Console.Write(jagged[2][i] + " ");

            Console.WriteLine();
OutPut

displaying first array
0 1
displaying second array
0 1 2
displaying third array
0 1
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-23-2007, 12:13 AM
mobilegeek mobilegeek is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 205
mobilegeek is on a distinguished road
Smile Re: Jagged Arrarys

Here is an example of how a Jagged Array works.

Code:
using System;
public class MyArrayclass
{
 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("");
  }
 }
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-30-2008, 03:48 AM
whisup whisup is offline
D-Web Trainee
 
Join Date: Sep 2008
Posts: 2
whisup is on a distinguished road
Default Re: Jagged Arrays in C#

Thanks for all your post, I have the same problem and now it has been sorted!
__________________
plastic card printing
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Arrays in PHP Sabari PHP Programming 126 10-01-2008 10:16 PM
Classic asp arrays and recordset ramesh123 ASP and ASP.NET Programming 1 12-02-2007 07:44 PM
Using arrays in stored procedures oxygen Database Support 1 11-26-2007 07:01 AM
Arrays in Java leoraja8 Java Programming 7 11-19-2007 12:23 AM
Java:Tutorial - Arrays pranky Java Programming 0 02-23-2007 11:54 PM


All times are GMT -7. The time now is 12:09 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0