You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

System.IndexOutOfRangeException异常求助:数组索引越界问题排查

解决System.IndexOutOfRangeException数组越界问题

看起来你遇到的数组越界问题,根源是某一行数据分割后得到的数组长度不足3,导致你在创建Student实例时访问strArray[0]、strArray[1]或strArray[2]时,这些索引根本不存在。

可能的场景分析

你的数据集虽然标称是4行,但大概率存在以下情况:

  • 某一行是空行,或者字段数量不足3个(比如只有ID和名字,没有姓氏)
  • 某些行的分隔符不是逗号,或者存在多余空格导致分割后出现空字符串(虽然这种情况一般不会直接导致长度不足,但也会影响后续逻辑)

解决方案:添加校验逻辑让代码更健壮

下面是修改后的代码,通过提前校验数组长度、处理异常数据,彻底避免越界问题:

class StudentS { 
    // 修正:指定泛型类型,避免非类型安全操作
    private List<Student> theStudentList;

    public bool PopulateStudents(string path) { 
        theStudentList = new List<Student>(); 
        bool flag = false; 
        try { 
            List<string[]> strArrayList = new List<string[]>(); 
            using (StreamReader streamReader = new StreamReader(path)) { 
                string str; 
                while ((str = streamReader.ReadLine()) != null) { 
                    // 可以加上Trim避免空行或前后空格干扰
                    if (string.IsNullOrWhiteSpace(str)) {
                        Console.WriteLine("警告:发现空白行,已跳过");
                        continue;
                    }
                    string[] strArray = str.Split(','); 
                    strArrayList.Add(strArray); 
                } 
            } 
            for (int index1 = 0; index1 < strArrayList.Count; ++index1) { 
                string[] strArray = strArrayList[index1]; 

                // 核心校验:确保基础字段至少有3个
                if (strArray.Length < 3) {
                    Console.WriteLine($"警告:第{index1+1}行数据格式错误,字段不足3个,已跳过");
                    continue;
                }

                Student student = new Student(strArray[0], strArray[1], strArray[2]); 
                int index2 = 3; 

                // 处理成绩:确保每次都能拿到成对的earned和possible值
                while (index2 + 1 < strArray.Length) { 
                    int earned, possible;
                    // 用TryParse替代Parse,避免非整数导致的格式异常
                    if (int.TryParse(strArray[index2], out earned) && int.TryParse(strArray[index2 + 1], out possible)) {
                        student.EnterGrade(earned, possible); 
                    } else {
                        Console.WriteLine($"警告:第{index1+1}行的成绩数据无效,已跳过该成绩项");
                    }
                    index2 += 2; 
                } 

                // 处理成绩字段数量为奇数的情况
                if (index2 < strArray.Length) {
                    Console.WriteLine($"警告:第{index1+1}行成绩字段数量是奇数,最后一个无效字段已忽略");
                }

                student.CalGrade(); 
                theStudentList.Add(student); 
            } 
        } catch (Exception e) { 
            flag = true; 
            Console.WriteLine(e); 
        } 
        return flag; 
    } 

    // 其余属性方法保持不变...
}

class Student { 
    private float average; 
    // 修正:用List<int>替代ArrayList,实现类型安全
    private List<int> Earned; 
    private string letterGrade; 
    private string nameFirst; 
    private string nameLast; 
    private List<int> Possible; 
    private string studentID; 

    // 构造函数保持不变...

    // 其余方法保持不变...
}

额外优化建议

  1. 把Student类中的ArrayList换成List<int>,避免后续类型转换的潜在问题,同时保证类型安全
  2. 读取数据时可以对每行做Trim()处理,避免前后空格导致的分割异常

内容的提问来源于stack exchange,提问作者Toby Z

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:57:09