7/25/2010

C# Array (Jagged Array)



Jagged Array:
Collection of one dimensional array.
syntax:
       data_type[]][] array_name = new data_type[row_size][];

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

In jagged array, we can specify only the rows.
Save the memory


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Arrays
{
    class Program
    {
        static void Main(string[] args)
        {
            int[][] a = new int[3][];

            a[0] = new int[] { 2, 3 };
            a[1] = new int[] { 7 };
            a[2] = new int[] { 21, 67, 45 };

            for (int i = 0; i < a.Length; i++)
            {
                Console.WriteLine("Array-" + i); 
                for (int j = 0; j < a[i].Length; j++)
                {
                    Console.WriteLine(a[i][j]);
                }
                Console.WriteLine();
            }

        }
    }
}

No comments:

Post a Comment