ACP
ACP

Reputation: 35268

What is a jagged array?

What is a jagged array (in c#)? Any examples and when should one use it....

Upvotes: 49

Views: 37522

Answers (7)

PriyankaS
PriyankaS

Reputation: 1

Jagged Array is multidimensional array with different different number of rows.

Upvotes: -2

Adiii
Adiii

Reputation: 60066

A jagged array is one in which you declare the number of rows during declaration but you declare number of columns during run time or also by user choice,simply its mean when you want different number of columns in each JAGGED array is suitable in that case

int[][] a = new int[6][];//its mean num of row is 6
        int choice;//thats i left on user choice that how many number of column in each row he wanna to declare

        for (int row = 0; row < a.Length; row++)
        {
           Console.WriteLine("pls enter number of colo in row {0}", row);
           choice = int.Parse(Console.ReadLine());
            a[row] = new int[choice];
            for (int col = 0; col < a[row].Length; col++)
            {
                a[row][col] = int.Parse(Console.ReadLine());
            }
        }

Upvotes: 2

Deepak Kumar
Deepak Kumar

Reputation: 9

Jagged array is an array with other arrays contained within.

A jagged array is a array in which the number of rows is fixed but the number of column is not fixed.

Code for jagged array in C# for window form application

int[][] a = new int[3][];

a[0]=new int[5];
a[1]=new int[3];
a[2]=new int[1];

int i;

for(i = 0; i < 5; i++)
{
    a[0][i] = i;
    ListBox1.Items.Add(a[0][i].ToString());
}

for(i = 0; i < 3; i++)
{
    a[0][i] = i;
    ListBox1.Items.Add(a[0][i].ToString());
}

for(i = 0; i < 1; i++)
{
    a[0][i] = i;
    ListBox1.Items.Add(a[0][i].ToString());
}

As you can see in the above program no of rows is fixed to 3, but the number of columns is not fixed. So we have taken three different value of columns i.e. 5, 3 and 1. The ListBox1 keyword used in this code is for the listbox that we will use in the window form to see the result by the click of button which will be also used in the window form. All the programming done here is on the button.

Upvotes: 0

Anthony Pegram
Anthony Pegram

Reputation: 126932

A jagged array is an array of arrays.

string[][] arrays = new string[5][];

That's a collection of five different string arrays, each could be a different length (they could also be the same length, but the point is there is no guarantee that they are).

arrays[0] = new string[5];
arrays[1] = new string[100];
...

This is different from a 2D array where it is rectangular, meaning each row has the same number of columns.

string[,] array = new string[3,5];

Upvotes: 59

Aayushi Jain
Aayushi Jain

Reputation: 2879

Although the best answer is chosen by the question owner but still I want to present the following code to make jagged array more clear.

using System;

class Program
{
static void Main()
 {
 // Declare local jagged array with 3 rows.
 int[][] jagged = new int[3][];

 // Create a new array in the jagged array, and assign it.
 jagged[0] = new int[2];
 jagged[0][0] = 1;
 jagged[0][1] = 2;

 // Set second row, initialized to zero.
 jagged[1] = new int[1];

 // Set third row, using array initializer.
 jagged[2] = new int[3] { 3, 4, 5 };

 // Print out all elements in the jagged array.
 for (int i = 0; i < jagged.Length; i++)
  {
    int[] innerArray = jagged[i];
    for (int a = 0; a < innerArray.Length; a++)
    {
    Console.Write(innerArray[a] + " ");
    }
    Console.WriteLine();
  }
 }
}

The output will be

1 2

0

3 4 5

Jagged arrays are used to store data in rows of varying length.

For more information check this post at MSDN blog.

Upvotes: 7

Tarik
Tarik

Reputation: 81781

You can find more information here : http://msdn.microsoft.com/en-us/library/2s05feca.aspx

Also :

A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays." The following examples show how to declare, initialize, and access jagged arrays.

The following is a declaration of a single-dimensional array that has three elements, each of which is a single-dimensional array of integers:

jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];

or

jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };

Upvotes: 5

Tarka
Tarka

Reputation: 4043

A jagged array is the same in any language, but it's where you have a 2+ dimensional array with different array lengths in the second and beyond array.

[0] - 0, 1, 2, 3, 4
[1] - 1, 2, 3
[2] - 5, 6, 7, 8, 9, 10
[3] - 1
[4] - 
[5] - 23, 4, 7, 8, 9, 12, 15, 14, 17, 18

Upvotes: 10

Related Questions