C#中的多维数组
本文关键字:数组 | 更新日期: 2023-09-27 17:59:45
例如:
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列命名为1月、4月、7月、10月?
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
当我点击一个按钮打印到TextBox时,会产生下面这样的输出吗?
January April July October
2014 1 2 3 4
2015 5 6 7 8
在尝试WinForms之前,请先从CLI开始。我个人喜欢在CLI中操作字符串,当我确信自己知道自己在做什么时,我才会将其翻译成WinForms。看看你的问题,我想你正在寻找某种日历程序
知道所有人都会从示例中学到最好的东西,下面是代码
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();
}
}
}