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 ?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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]; |
| |||
| 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(); 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! |
| |||
| 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("");
}
}
} |
| |||
| Thanks for all your post, I have the same problem and now it has been sorted!
__________________ plastic card printing |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |