读取txt文件时Array2D的索引超出限制

时间:2018-09-09 08:37:32

标签: c# file-io

我是C#编程的新手,我正尝试从txt读取文件,但是每次执行这段代码时,索引都会超出限制,即使我进行了调试也看不到找到解决方案在这种情况下。

我确定这很简单,但是我是C#的新手,感谢您看代码。

  static void importFiles(string[,] matrix)
            {
                var path = @"export/file.txt";
        int start = getInsertIndex(matrix);


  if (File.Exists(path))
        {
            string[] fileLines = File.ReadAllLines(path);

            if (fileLines.Length == matrix.GetLength(0))
                {
                string[,] map = new string[fileLines.Length, matrix.GetLength(1)];

                for (int i = 0; i < fileLines.Length; i++)
                {
                    string line = fileLines[i];
                    for (int j = 0; j < matrix.GetLength(1); j++)
                    {
                        string[] split = line.Split(';');
                        matrix[start, j] = split[j]?.Trim();
                    }
                    start++;


                }

            } }

 static int getInsertIndex(string[,] matrix)
        {
            for (int j = 0; j < matrix.GetLength(0); j++)
            {
                if (string.IsNullOrEmpty(matrix[j, 0])) return j;
            }

            return -1;
        }

我更改了代码,但是当使用嵌套的可视化矩阵内部内容时,我什么也没有。我似乎无法理解为什么执行该方法,而矩阵内部什么也没得到。

2 个答案:

答案 0 :(得分:1)

如果矩阵中的行数与读取的行数不同(例如,抛出异常),则可以处理代码中的情况:

static void importFiles(string[,] matrix)
{
    var path = @"export/file.txt";
    if (!File.Exists(path)) return;

    string[] fileLines = File.ReadAllLines(path);

    if(fileLines.Length != matrix.GetLength(0))
        // here you have couple of opitions
        // throw new ArgumentException("Number of rows in passed matrix is different from number of lines in a file!");
        // or just do nothing:
        return;

    string[,] map = new string[fileLines.Length, fileLines[0].Split(';').Length];

    for (int i = start; i < fileLines.Length; i++)
    {
        string line = fileLines[i];
        for (int j = 0; j < matrix.GetLength(1); j++)
        {
            string[] split = line.Split(';');
            matrix[i, j] = split[j]?.Trim();
        }
    }   
}

或者您可以在方法中初始化matrix数组:

string[] fileLines = File.ReadAllLines(path);
// c is number of columns, that you are using now
string[,] matrix = new string[fileLines.Length, c];

答案 1 :(得分:1)

在我看来,您不知道文件的开头,并且不知道分割行时的项目数。在这种情况下,我不会使用固定矩阵,而会使用字典或类似工具。

static void Main(string[] args)
        {
            Dictionary<int, string[]> filesData = new Dictionary<int, string[]>();
            importFiles(filesData);


        }

        static void importFiles(Dictionary<int, string[]> filesData)
        {
            var path = @"export/file.txt";


            if (File.Exists(path))
            {
                string[] fileLines = File.ReadAllLines(path);

                for (int i = 0; i < fileLines.Length; i++)
                {
                    string line = fileLines[i];
                    string[] split = line.Split(';');

                    for (int j = 0; j < split.Length; j++)
                    {
                        split[j] = split[j].Trim();
                    }
                    int index = filesData.Count == 0 ? 0: filesData.Keys.Max(); 
                    filesData.Add(index + 1, split);

                }
            }
        }