Jagged Array

ashishkumar008 1 Tallied Votes 132 Views Share

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".

NOTE:- I'm using Single dimensional jagged array. Actually I am using single and multi dimensional arrays as a element of single dimensional jagged array. You can use "foreach" to print jagged arrays.

Paste PrintSingleDimentionalJaggedArray() and PrintMultiDimJaggedArray() methods in main method. You can download the example JaggedArray.zip file which is given below...

using System;
namespace JaggedArraySingle
{
    class SingleDimentionalJaggedArray
    {
        public void PrintSingleDimentionalJaggedArray()
        {
            int[][] myjaggedarray = new int[2][];
            myjaggedarray[0] = new int[5];
            myjaggedarray[1] = new int[2];

            myjaggedarray[0] = new int[] { 1, 3, 5, 7, 9 };
            myjaggedarray[1] = new int[] { 6, 8 };

            Console.WriteLine("-------------------------------------------------");
            Console.WriteLine("This Is The Example Of Single Dimensional Array Element Of Single Dimentioanl Jagged Array");
            Console.WriteLine("-------------------------------------------------");

            for (int i = 0; i < myjaggedarray.Length; i++)
            {
                Console.WriteLine("This Is The " + i + " Positioned Element Of Single Dimentional Jagged Array \n");

                for (int j = 0; j < myjaggedarray[i].Length; j++)
                {
                    Console.Write(myjaggedarray[i][j] + " ");
                }
                Console.WriteLine("\n");
            }
        }
    }
}






using System;
namespace JaggedArrayMulti
{
    class MultiDimentionalJaggedArray
    {

        public void PrintMultiDimJaggedArray()
        {
            int[][,] myjaggedarraymutidim = new int[2][,];
            myjaggedarraymutidim[0] = new int[4, 2];
            myjaggedarraymutidim[1] = new int[2, 2];

            myjaggedarraymutidim[0] = new int[,] { { 1, 2 }, { 2, 3 }, { 5, 6 }, { 7, 8 } };
            myjaggedarraymutidim[1] = new int[,] { { 5, 6 }, { 2, 4 } };

            Console.WriteLine("-------------------------------------------------");
            Console.WriteLine("This Is The Example Of Multi Dimensional Array Element Of Single Dimentioanl Jagged Array");
            Console.WriteLine("-------------------------------------------------");

            for (int i = 0; i < myjaggedarraymutidim.Length; i++)
            {
                Console.WriteLine("This Is The " + i + " Positioned Element Of Multi Diementional Jagged Array \n");

                for (int j = 0; j < myjaggedarraymutidim[i].GetLength(0); j++)
                {
                    for (int k = 0; k < myjaggedarraymutidim[i].GetLength(1); k++)
                    {
                        Console.Write(" "+myjaggedarraymutidim[i][j, k]+" ");
                    }

                    Console.WriteLine();
                }

                Console.WriteLine("\n");
            }
        }

    }
}
ashishkumar008 2 Light Poster

Remeber main method is missing.

Shlesh 0 Newbie Poster

thank's example is working...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.