2016-08-02 86 views
-6

例如:的多维数组

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

for (int Row = 0; Row < multiArray.GetLength(0); Row++) 
{ 

    for (int Col = 0; Col < multiArray.GetLength(1); Col++) 
    { 
     TextBox.Text += multiArray[Row, Col] + " "; 
    } 

    TextBox.Text += "\r\n"; 
} 

上面的代码会产生:

1 2 3 4 
5 6 7 8 

如何命名我的2行作为2014年,2015年和名字我的4列在一月份,四月,七月,十月?

Value [2014, January] or index [0, 0] = 1, 
Value [2014, April] or index [0, 1] = 2, 
Value [2014, July] or index [0, 2] = 3, 
Value [2014, October] or index [0, 3] = 4, 
Value [2015, January] or index [1, 0] = 5, 
Value [2015, April] or index [1, 1] = 6, 
Value [2015, July] or index [1, 2] = 7, 
Value [2015, October] or index [1, 3] = 8 

而当我通过点击一个按钮打印出来的文本框会产生像下面的输出?

 January April July October 
2014 1  2  3  4 
2015 5  6  7  8 
+3

欢迎来到StackOverflow!你的问题有点不清楚。数组不产生输出。请澄清你试图达到的目标并展示你到目前为止所尝试的内容。 –

+0

如果这是你想要的,你应该澄清,你想将表转换为二维查找表/字典;如果不是的话:解释 –

+0

你想google的主题是二维数组。例如http://stackoverflow.com/questions/3814145/how-can-i-declare-a-two-dimensional-string-array –

回答

0

在尝试使用CLI启动WinForms之前。我个人喜欢在CLI中操纵字符串,当我确定我知道我在做什么,然后才将其转换为WinForms.Looking你的问题,我假设你正在寻找做某种日历程序。
知道所有的人都会从例子中学到最好,这里是代码。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace StackOverflow 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string[] months = { "Jan","Feb","Mar" }; 
      int[] years = { 2015, 2014, 2013 }; 
      int[,] important = { { 1, 2, 3 }, { 3, 4, 6 }, { 8, 16, 1 } }; 

      Console.Write("\t"); 
      foreach (string month in months) 
      { 
       Console.Write(month + "\t"); 
      } 
      Console.WriteLine(); 
      Console.WriteLine(); 
      foreach (int year in years) 
      { 
       Console.Write(year.ToString()); 
       foreach (var month in months) 
       { 
        Console.Write("\t" + important[years.ToList<int>().IndexOf(year),months.ToList<string>().IndexOf(month)].ToString()); 
       } 
       Console.WriteLine(); 
       Console.WriteLine(); 
      } 
      Console.WriteLine(); 
      Console.ReadKey(); 

     } 
    } 
}