为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
阿晨1998 2022-01-09 15:30:04
我是 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 来可视化矩阵内部的内容时,我什么也没得到。我似乎无法理解为什么要执行该方法,并且在矩阵中什么也没有。
查看完整描述

2 回答

?
DIEA

TA贡献1820条经验 获得超3个赞

在我看来,您事先并不知道文件的行数,并且在拆分行时也不知道项目的数量。在这种情况下,我不会使用固定矩阵,而是使用字典等。


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);


                }

            }

        }


查看完整回答
反对 回复 2022-01-09
?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

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


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];


查看完整回答
反对 回复 2022-01-09
  • 2 回答
  • 0 关注
  • 167 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号