C# 将数组转换为二维数组时出现的问题

C# 将数组转换为二维数组时出现的问题,c#,arrays,multidimensional-array,C#,Arrays,Multidimensional Array,我有一个名为Labyrint的文本文件,我将它读入一个char数组。之后,我将char数组读入一个不带\n(换行符)的列表中。之后,我将列表转换为数组。现在我希望这个数组是一个二维数组,但是我该怎么做呢。这是一张大小为21x21的标签图片。下面是我的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Threa

我有一个名为Labyrint的文本文件,我将它读入一个char数组。之后,我将char数组读入一个不带\n(换行符)的列表中。之后,我将列表转换为数组。现在我希望这个数组是一个二维数组,但是我该怎么做呢。这是一张大小为21x21的标签图片。下面是我的代码:

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

namespace ConsoleApplication25
{
    class Program
    {
        static char[] FloridaArray;
        static string DenverString;
        static string[,] names = new string[21, 21];
        static List<object> wrd = new List<object>();

        static void Main(string[] args)
        {
            //Increase Console Buffer Height
            Console.BufferHeight = Int16.MaxValue - 1;
            DenverString = ConvertStringArrayToString();
            FloridaArray = DenverString.ToArray();
            Console.WriteLine(DenverString);

            for (int i = 0; i < FloridaArray.Length; i++)
            {
                if (FloridaArray[i] != '\n')
                {
                    wrd.Add(FloridaArray[i].ToString());
                }
            }

            foreach (object o in wrd)
            {
                Console.WriteLine(o);
            }

            //Here I check what index 21 contain in the list called wrd
            Console.WriteLine("Here I check if index 21 contain character B:       " + wrd[21]);
            Console.WriteLine(wrd.GetType());

            // Here I convert the list called wrd to an array.
            object[] myArray = wrd.ToArray();
            // Here I check what character is in index 21 in the array called myArray.
            Console.WriteLine(myArray[21]);
            //Here I look up the data type of myArray
            Console.WriteLine(myArray.GetType());

            for (int j = 0; j < 21; j++)
            {
                for (int i = 0; i <= 21; i++)
                {
                    // how do I put the value from my char array into the two dimensional array?
                    names[j, i] = myArray[i].ToString();
                    Console.WriteLine(j + " names " + i);    
                }
            }
        }

        static string ConvertStringArrayToString()
        {
            // Concatenate all the elements into a StringBuilder.
            StringBuilder builder = new StringBuilder();
            foreach (var value in File.ReadAllLines("Labyrint.txt", Encoding.UTF8).Skip(1))
            {
                builder.Append(value);
                builder.Append('\n');
            }
            return builder.ToString();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
使用System.Threading.Tasks;
命名空间控制台应用程序25
{
班级计划
{
静态字符数组;
静态字符串DenverString;
静态字符串[,]名称=新字符串[21,21];
静态列表wrd=新列表();
静态void Main(字符串[]参数)
{
//增加控制台缓冲区高度
Console.BufferHeight=Int16.MaxValue-1;
DenverString=ConvertStringArrayToString();
FloridaArray=DenverString.ToArray();
控制台写入线(DenverString);
for(int i=0;i对于(int i=0;i我认为这里的重点是需要计算主数组的索引。从嵌套循环索引器中,需要的值是:

j*21 + i
这使循环看起来像这样:

for (int j = 0; j < 21; j++)
{
    for (int i = 0; i <= 21; i++)
    {
        names[j, i] = myArray[j*21 + i].ToString();
        Console.WriteLine(j + " names " + i);    
    }
}
for(int j=0;j<21;j++)
{

对于(int i=0;i我认为这里的重点是需要计算主数组的索引。从嵌套循环索引器中,需要的值是:

j*21 + i
这使循环看起来像这样:

for (int j = 0; j < 21; j++)
{
    for (int i = 0; i <= 21; i++)
    {
        names[j, i] = myArray[j*21 + i].ToString();
        Console.WriteLine(j + " names " + i);    
    }
}
for(int j=0;j<21;j++)
{

对于(int i=0;我感谢您的回复@DavidG,它解决了问题。我唯一需要更改的是21到20(因为我在读取文件时跳过了1行),但我离开了myArray[j*21+i]像21一样,否则它会给我错误的索引。如果你看到这个评论,你能解释一下为什么用21乘以j吗?这部分我真的不明白?谢谢你的回复@DavidG,它解决了问题。我唯一需要更改的是21到20(因为我在文件中阅读时跳过了1行),但我留下了我的数组[j*21+I]和21一样,否则会给我错误的索引。如果你看到这条评论,你能解释一下为什么用21乘以j吗?我真的不明白这部分?