none
How to iterate in 2d array when we have there employee info RRS feed

  • Question

  • suppose i have 2d array and there i have employee info like id, name, salary, dob etc.

    now how could i iterate array to extract each employee info and print it on screen using console.writeline?

    share some code example.


    // First
                string[,] arr1 = {
                           { "aa", "aaa" },
                           { "bb", "bbb" }
                       };
    
                // Second
                string[][] arr2 = new[] {
                    new[] { "aa", "aaa" },
                    new[] { "bb", "bbb" }
                };
    
                // Iterate through first
                for (int x = 0; x <= arr1.GetUpperBound(0); x++)
                    for (int y = 0; y <= arr1.GetUpperBound(1); y++)
                        Console.Write(arr1[x, y] + "; ");
    
                Console.WriteLine(Environment.NewLine);

    see above code.

    what is difference between these two array declaration ? both the array is 2d array ?

                string[,] arr1 = {
                           { "aa", "aaa" },
                           { "bb", "bbb" }
                       };
    
                // Second
                string[][] arr2 = new[] {
                    new[] { "aa", "aaa" },
                    new[] { "bb", "bbb" }
                };

    • Edited by Sudip_inn Wednesday, December 19, 2018 8:09 PM
    Wednesday, December 19, 2018 7:52 PM

Answers

  • Hi Sddip_inn,

    Thank you for posting here.

    For your question, during creation, two-dimensional array [,] generates storage for all the locations together. But, [][] is called jagged array and it is basically an array of arrays. When you create it, you first only specify the size of the first dimension or not. You will need to go through all the base arrays to instantiate the sub array.

    For example, I use two arrays for reference.

    string[,] arr1 = {
                           { "aa", "aaa" },
                           { "bb", "bbb" }
                       };
    
                // Second
                string[][] arr2_1 = new string [3][];
                string[][] arr2_2 = new[] {
                    new[] { "a", "aa" },
                    new[] { "b", "bb","bbb" },
                    new[] { "c", "cc","ccc","cccc" }
                };

    Even if their "length" is the same, we could not call them in the same way.

     string[,] arr1 = {
                           { "aa", "aaa" },
                           { "bb", "bbb" }
                       };
                for (int i = 0; i < arr1.GetLength(0); i++)
                {
                    for (int j = 0; j < arr1.GetLength(1); j++)
                    {
                        Console.WriteLine("arr1[{0},{1}]:{2}", i, j, arr1[i, j]);
                    }
                }
                // Second
                string[][] arr2 = new[] {
                    new[] { "aa", "aaa" },
                    new[] { "bb", "bbb" }
                };
    
                for (int i = 0; i < arr2.Length; i++)
                {
                    for (int j = 0; j < arr2[i].Length; j++)
                    {
                        Console.WriteLine("arr2[{0}][{1}]={2}", i, j, arr2[i][j]);
                    }
                }
             

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, December 20, 2018 8:52 AM
    Thursday, December 20, 2018 5:54 AM
    Moderator

All replies


  • what is difference between these two array declaration ? both the array is 2d array ?

                string[,] arr1 = {
                           { "aa", "aaa" },
                           { "bb", "bbb" }
                       };
    
                // Second
                string[][] arr2 = new[] {
                    new[] { "aa", "aaa" },
                    new[] { "bb", "bbb" }
                };

    Multidimensional Arrays vs. Jagged Arrays

    Arrays (C# Programming Guide)
    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/

    Multidimensional Arrays (C# Programming Guide)
    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/multidimensional-arrays

    Jagged Arrays (C# Programming Guide)
    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/jagged-arrays

    - Wayne

    Wednesday, December 19, 2018 8:56 PM
  • Hi Sddip_inn,

    Thank you for posting here.

    For your question, during creation, two-dimensional array [,] generates storage for all the locations together. But, [][] is called jagged array and it is basically an array of arrays. When you create it, you first only specify the size of the first dimension or not. You will need to go through all the base arrays to instantiate the sub array.

    For example, I use two arrays for reference.

    string[,] arr1 = {
                           { "aa", "aaa" },
                           { "bb", "bbb" }
                       };
    
                // Second
                string[][] arr2_1 = new string [3][];
                string[][] arr2_2 = new[] {
                    new[] { "a", "aa" },
                    new[] { "b", "bb","bbb" },
                    new[] { "c", "cc","ccc","cccc" }
                };

    Even if their "length" is the same, we could not call them in the same way.

     string[,] arr1 = {
                           { "aa", "aaa" },
                           { "bb", "bbb" }
                       };
                for (int i = 0; i < arr1.GetLength(0); i++)
                {
                    for (int j = 0; j < arr1.GetLength(1); j++)
                    {
                        Console.WriteLine("arr1[{0},{1}]:{2}", i, j, arr1[i, j]);
                    }
                }
                // Second
                string[][] arr2 = new[] {
                    new[] { "aa", "aaa" },
                    new[] { "bb", "bbb" }
                };
    
                for (int i = 0; i < arr2.Length; i++)
                {
                    for (int j = 0; j < arr2[i].Length; j++)
                    {
                        Console.WriteLine("arr2[{0}][{1}]={2}", i, j, arr2[i][j]);
                    }
                }
             

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, December 20, 2018 8:52 AM
    Thursday, December 20, 2018 5:54 AM
    Moderator