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

我是 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;

        }

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


阿晨1998
浏览 168回答 2
2回答

DIEA

在我看来,您事先并不知道文件的行数,并且在拆分行时也不知道项目的数量。在这种情况下,我不会使用固定矩阵,而是使用字典等。static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dictionary<int, string[]> filesData = new Dictionary<int, string[]>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; importFiles(filesData);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; static void importFiles(Dictionary<int, string[]> filesData)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var path = @"export/file.txt";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (File.Exists(path))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] fileLines = File.ReadAllLines(path);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < fileLines.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string line = fileLines[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] split = line.Split(';');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < split.Length; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; split[j] = split[j].Trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int index = filesData.Count == 0 ? 0: filesData.Keys.Max();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filesData.Add(index + 1, split);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }

鸿蒙传说

如果矩阵中的行数与读取的行数不同(例如抛出异常),您可以在代码中处理情况:static void importFiles(string[,] matrix){&nbsp; &nbsp; var path = @"export/file.txt";&nbsp; &nbsp; if (!File.Exists(path)) return;&nbsp; &nbsp; string[] fileLines = File.ReadAllLines(path);&nbsp; &nbsp; if(fileLines.Length != matrix.GetLength(0))&nbsp; &nbsp; &nbsp; &nbsp; // here you have couple of opitions&nbsp; &nbsp; &nbsp; &nbsp; // throw new ArgumentException("Number of rows in passed matrix is different from number of lines in a file!");&nbsp; &nbsp; &nbsp; &nbsp; // or just do nothing:&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; string[,] map = new string[fileLines.Length, fileLines[0].Split(';').Length];&nbsp; &nbsp; for (int i = start; i < fileLines.Length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string line = fileLines[i];&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < matrix.GetLength(1); j++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] split = line.Split(';');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matrix[i, j] = split[j]?.Trim();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;}或者你可以matrix在你的方法中初始化你的数组:string[] fileLines = File.ReadAllLines(path);// c is number of columns, that you are using nowstring[,] matrix = new string[fileLines.Length, c];
打开App,查看更多内容
随时随地看视频慕课网APP