2014-10-28 74 views
1

如何獲取多維數組中數組的值...在另一個數組中?獲取數組中多維數組中數組的值

我想在下面的代碼中以升序獲得test的值。

我試過使用forforeach循環,但在引用多維數組元素時遇到了問題。

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(); 
} 

回答

2

爲什麼不:

Console.WriteLine("{0}", am[ama].ToString()); 
2

而不是使用foreach你也可以使用for如下:

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()); 
     } 
     } 
    } 
2

萊昂內爾,

這裏是你的代碼的工作:

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(); 
    } 

不需要在打印語句中引用整個數組。你只需要引用內部循環。希望有所幫助。

祝福, Bill