Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i print the values of this jagged array.
here jagged array size is 3.just bcoz each position of jagged array points to different size 2D array. i m not able to print its values. when i run this program only 1 and 3 displayed and its shows following error
" system.index.outofrange exception"

What I have tried:

C#
static void Main(string[] args)
        {
            
            int[][,] jaggedArray4 = new int[3][,] 
        {
            new int[,] { {1,3}, {5,7} },
            new int[,] { {0,2}, {4,6}, {8,10} },
            new int[,] { {11,22}, {99,88}, {0,9} } 
        };
Console.WriteLine("Your Data\n");
            for (int i = 0; i < jaggedArray4.Length; i++)
            {
                for (int j = 0; j < jaggedArray4[i].Length; j++)
                {
                    for (int k = 0; k < jaggedArray4[j].Length;k++)
                    {
                        Console.WriteLine("jaggedposition i[{0}] 2D array[{1},{2}] is {3}", i, j, k, jaggedArray4[i][j,k]);

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

                Console.WriteLine("\n");
            }
Posted
Updated 8-Dec-16 22:48pm
v2

1 solution

Try:
C#
for (int i = 0; i < jaggedArray4.Length; i++)
    {
    int[,] line = jaggedArray4[i];
    for (int j = 0; j < line.GetLength(0); j++)
        {
        for (int k = 0; k < line.GetLength(1); k++)
            {
            Console.WriteLine("jaggedposition i[{0}] 2D array[{1},{2}] is {3}", i, j, k, jaggedArray4[i][j, k]);
            }
        Console.WriteLine("\n");
        }
    Console.WriteLine("\n");
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900