Working with Jagged Arrays:-

  • An Array which contains another Array within it is known as Jagged Array
  • A Jagged Array is known as Array of Arrays
     Working with Jagged Array :- 

  


Syntax: Data Type[ ][ ] Array Name=new Data Type [Rowsize][ ];
Ex: int[ ][ ]A=new int[3][ ];
A[0]=new int[3]{95,9,19 };
A[1]=new int[4]{29,39,49,59};
A[2]=new int[5]{69,79,89,99,88};

Identification of elements
A[0][0] --  95 ;  A[0][1] -- 9  ;  A[0][2] --  19;

A[1][0] --  29          ;  A[1][1]  --  39;  A[1][2]  --  49  ;  A[1][3]  --  59;

A[2][0]  --  69  ;  A[2][1]  --  79  ;  A[2][2]  -- 89  ;  A[2][3]  --  99;

A[2][4]  --  88

 Example :-

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

namespace CAArrays
{
    class ClsJArray
    {
        static void Main()
        {

            int[ ][ ] A = new int[3][ ];
            A[0] = new int[3] { 95, 9, 19 };
            A[1] = new int[4] { 29, 39, 49, 59 };
            A[2] = new int[5] { 69, 79, 89, 99, 88 };
            Console.WriteLine("Elements of Jagged Array Are " );
            for (int R = 0; R < A.Length; R++)
            {
                for (int c = 0; c < A[R].Length; c++)
                {
                    Console.Write(A[R][c]+"    ");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}


   Output:-



Example with foreach Loop:-

         
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//Program to work with Jagged Array using foreach Loop
namespace CAArrays
{
    class ClsJArray2
    {
        static void Main()
        {
            int[][] A = new int[3][];
            A[0] = new int[3] { 95, 9, 19 };
            A[1] = new int[4] { 29, 39, 49, 59 };
            A[2] = new int[5] { 69, 79, 89, 99, 88 };
            Console.WriteLine("Elements of Jagged Array Are:-");
            foreach(int[] j in A)
            {
                foreach (int i in j)
                {
                    Console.Write(i +"   ");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}


              
  output:-
             


No comments:

Post a Comment