【问题标题】:Multi dimensional arrays in C#C#中的多维数组
【发布时间】:2016-12-07 13:21:05
【问题描述】:

例如:

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 列命名为 January、April、July、October?

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

【问题讨论】:

  • 欢迎来到 StackOverflow!你的问题有点不清楚。数组不产生输出。请说明您要达到的目标并展示您迄今为止所做的尝试。
  • 如果这是你想要的,你应该澄清你想要将表格转换为二维查找表/字典;如果不是:解释
  • 您要搜索的主题是二维数组。例如stackoverflow.com/questions/3814145/…
  • 您的请求存在以下问题:1) 多类型= 您希望单个数组(更多是二维数组或矩阵)同时包含字符串和整数。 2)访问=你想同时获得行和列,这不能用数组来完成,这不是它们的工作方式
  • 请重新打开我的问题,因为我已经比以前更清楚地解释了。谢谢。

标签: c#


【解决方案1】:

在试用 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();

        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-21
    • 2014-06-20
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多