通过for循环在c#中打印2d数组

问题描述:

这是我的归档函数,它从文件中获取数据并将其放置在数组中:

This is my filing function that takes data from the file and places it in the array:

public void populate_grid_by_file()
    {
        String store_data_from_file = string.Empty;
        try
        {
            using (StreamReader reader = new StreamReader("data.txt"))
            {
                store_data_from_file = reader.ReadToEnd().ToString();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        string[] line = store_data_from_file.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < line.Length; i++)
        {
            string[] alphabet = store_data_from_file.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
            for (int j = 0; j < alphabet.Length; j++)
            {
                Sodoku_Gri[i, j] = alphabet[j];
            }
        }            
    }

这是文件中写的内容:

1--2--3--

3-4-4-5--

-7-3-4---

7--5--3-6

--7---4--

3-2--4-5-

------3--

2-6--7---

4---4--3-

这是我要打印的内容:

1 - - 2 - - 3 - -
3 - 4 - 4 - 5 - -
- 7 - 3 - 4 - - -
7 - - 5 - - 3 - 6
- - 7 - - - 4 - - 
3 - 2 - - 4 - 5 - 
- - - - - - 3 - -
2 - 6 - - 7 - - - 
4 - - - 4 - - 3 - 

我想这样做:

public void display_grid()
    {
        for (int row = 0; row < Sodoku_Gri.GetLength(0); row++)
        {
            for (int col = 0; col < Sodoku_Gri.GetLength(1); col++)
            {

                Console.Write(Sodoku_Gri[row, col]);
                Console.Write("  ");
            }
            Console.WriteLine();
        }
    }

我只是不明白为什么二维数组在最后一行中额外打印了------------印刷了9次!

I just can't understand why the 2d array printed 9 times with additional ------------ in the last row!

应该是一个二维数组,每个元素之间都有间距,就像我在文件中显示的数据一样,但是没有间距.

It should be a 2d array with spacing between each element like I showed the data in the file but that is without spacing.

您正在将数据分割成几行,并且看来您打算在内部循环中处理当前行中的字符.但是,您实际上是在为每行迭代处理整个文件,这就是为什么您的输出包含所有文件字符*行数的原因.请尝试以下调整:

you are splitting the data into lines, and it appears your intention in the inner loop is to process the characters in the current line. However, you are actually processing the entire file for each line iteration, which is why your output contains all the file's characters * the number of lines. Try this adjustment instead:

   public void populate_grid_by_file()
        {
            String store_data_from_file = string.Empty;
            try
            {
                using (StreamReader reader = new StreamReader("data.txt"))
                {
                    store_data_from_file = reader.ReadToEnd().ToString();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            string[] line = store_data_from_file.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < line.Length; i++)
            {
                //string[] alphabet = store_data_from_file.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
                string[] alphabet = line[i].Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries)
                for (int j = 0; j < alphabet.Length; j++)
                {
                    Sodoku_Gri[i, j] = alphabet[j];
                }
            }            
        }