1

How can I get the value of an array in a multidimensional array...in another array?

I want to get the value of test in ascending order in the following code.

I tried to use a for and foreach loops but have had problems when it comes to referencing an element of the multidimensional array.

static void Main(string[] args)
{
    string[][,][] test = { new string[,][]{{
                                               new string[]{"test1","test2","test3"},
                                               new string[]{"test4","test6","test6"}
                                           },
                                           {
                                               new string[]{"test7","test7","test9"},
                                               new string[]{"test10","test11","test12"}
                                           }},
                           new string[,][]{{
                                               new string[]{"test13","test14","test15"},
                                               new string[]{"test16","test17","test18"}
                                           },
                                           {
                                               new string[]{"test19","test20","test21"},
                                               new string[]{"test22","test23","test24"}
                                           }}
                         };
    for (int a = 0; a < test.Count(); a++ )
    {
        foreach(var am in test[a])
        {
            for (int ama = 0; ama < am.Count(); ama++)
            {
                Console.WriteLine("{0}",test[a][0,0][ama].ToString()); //what should I put in [0,0]?
            }
        }
    }
    Console.ReadKey();
}

3 Answers 3

2

why not:

Console.WriteLine("{0}", am[ama].ToString());
Sign up to request clarification or add additional context in comments.

Comments

2

Instead of using foreach you could also use for as follows:

  for ( int a = 0; a < test.Count(); a++ )
  {
    string[,][] ta = test[a];
    for( int i1 = 0; i1 < ta.GetLength( 0 ); i1++ )
    {
      for( int i2 = 0; i2 < ta.GetLength( 1 ); i2++ )
      {
        string[] am = ta[i1, i2];
        for ( int ama = 0; ama < am.Count(); ama++ )
        {
          Console.WriteLine( "{0}", test[ a ][ i1, i2 ][ ama ].ToString() );
        }
      }
    }

Comments

2

Leonel,

Here is your code working:

static void Main(string[] args)
    {
        string[][,][] test = { new string[,][]{{
                                                   new string[]{"test1","test2","test3"},
                                                   new string[]{"test4","test5","test6"}
                                               },
                                               {
                                                   new string[]{"test7","test8","test9"},
                                                   new string[]{"test10","test11","test12"}
                                               }},
                               new string[,][]{{
                                                   new string[]{"test13","test14","test15"},
                                                   new string[]{"test16","test17","test18"}
                                               },
                                               {
                                                   new string[]{"test19","test20","test21"},
                                                   new string[]{"test22","test23","test24"}
                                               }}
                             };
        for (int a = 0; a < test.Count(); a++)
        {
            foreach(string[] am in test[a])
            {
                for (int ama = 0; ama < am.Count(); ama++)
                {
                    Console.WriteLine("{0}", am[ama].ToString()); //Reference to the inside loop
                }
            }
        }
        Console.ReadKey();
    }

There is no need to reference the entire array in your print statement. You only need to reference the inside loop. Hope that helps.

Best Wishes, Bill

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.