C# 索引超出了数组的边界?
我一直收到这样一条消息“System.IndexOutOfRangeException:Index超出了数组的边界”。当尝试运行我构建的程序时,该程序使用了异常捕获程序 班级学生 { 私人名单学生名单C# 索引超出了数组的边界?,c#,arrays,exception,try-catch,C#,Arrays,Exception,Try Catch,我一直收到这样一条消息“System.IndexOutOfRangeException:Index超出了数组的边界”。当尝试运行我构建的程序时,该程序使用了异常捕获程序 班级学生 { 私人名单学生名单 public bool PopulateStudents(string path) { theStudentList = new List<Student>(); bool flag = false; try
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)
{
string[] strArray = str.Split(',');
strArrayList.Add(strArray);
}
}
for (int index1 = 0; index1 < strArrayList.Count; ++index1)
{
string[] strArray = strArrayList[index1];
Student student = new Student(strArray[0], strArray[1], strArray[2]); **where the error is**
int index2 = 3;
while (index2 < strArray.Length)
{
student.EnterGrade(int.Parse(strArray[index2]), int.Parse(strArray[index2 + 1]));
index2 += 2;
}
student.CalGrade();
theStudentList.Add(student);
}
}
catch (Exception e)
{
flag = true;
Console.WriteLine(e);
}
return flag;
}
public int ListLength
{
get
{
return theStudentList.Count;
}
}
public float StudentAverage(int index)
{
return theStudentList.ElementAt(index).Average;
}
public string StudentGrade(int index)
{
return theStudentList.ElementAt(index).LetterGrade;
}
public string StudentID(int index)
{
return theStudentList.ElementAt(index).ID;
}
public string StudentLastName(int index)
{
return theStudentList.ElementAt(index).NameLast;
}
}
class Student
{
private float average;
private ArrayList Earned;
private string letterGrade;
private string nameFirst;
private string nameLast;
private ArrayList Possible;
private string studentID;
public Student(string id)
{
studentID = null;
nameFirst = null;
nameLast = null;
Earned = new ArrayList();
Possible = new ArrayList();
}
public Student(string id, string first)
{
studentID = id;
nameFirst = null;
nameLast = null;
Earned = new ArrayList();
Possible = new ArrayList();
}
public Student(string id, string first, string last)
{
nameFirst = first;
nameLast = last;
studentID = id;
Earned = new ArrayList();
Possible = new ArrayList();
}
public float Average
{
get
{
return average;
}
}
public string ID
{
get
{
return studentID;
}
}
public string LetterGrade
{
get
{
return letterGrade;
}
}
public string NameFirst
{
get
{
return nameFirst;
}
set
{
nameFirst = value;
}
}
public string NameLast
{
get
{
return nameLast;
}
set
{
nameLast = value;
}
}
public void CalGrade()
{
int num1 = 0;
int num2 = 0;
foreach (int num3 in Earned)
num1 += num3;
foreach (int num3 in Possible)
num2 += num3;
average = num1 / (float)num2;
average = (float)Math.Round(average, 2);
if (average >= 0.9)
letterGrade = "A";
if (average >= 0.8 && average < 0.9)
letterGrade = "B";
if (average >= 0.7 && average < 0.8)
letterGrade = "C";
if (average >= 0.6 && average < 0.7)
letterGrade = "D";
if (average >= 0.6)
return;
letterGrade = "U";
}
public void EnterGrade(int earnedValue, int possValue)
{
Earned.Add(earnedValue);
Possible.Add(possValue);
}
}
public bool PopulateStudents(字符串路径)
{
学生列表=新列表();
布尔标志=假;
尝试
{
List strArrayList=新列表();
使用(StreamReader StreamReader=新StreamReader(路径))
{
字符串str;
而((str=streamReader.ReadLine())!=null)
{
字符串[]strArray=str.Split(',');
strArrayList.Add(strArray);
}
}
对于(int index1=0;index1=0.9)
letterGrade=“A”;
如果(平均值>=0.8&&平均值<0.9)
letterGrade=“B”;
如果(平均值>=0.7&&平均值<0.8)
letterGrade=“C”;
如果(平均值>=0.6&&平均值<0.7)
letterGrade=“D”;
如果(平均值>=0.6)
返回;
letterGrade=“U”;
}
公共等级(int earnedValue、int possValue)
{
挣得的。加上(挣得的价值);
可能。增加(价值);
}
}
我不确定我做错了什么。谢谢你的帮助
编辑:我在这里添加了大部分代码,希望这能更好地回答您的问题。我正在处理的数据集是4行,它们被抓取到student数组中
index2+1
可能超出下面表达式strArray[index2+1]
的范围:
while (index2 < strArray.Length)
{
student.EnterGrade(int.Parse(strArray[index2]), int.Parse(strArray[index2 + 1]));
index2 += 2;
}
while(index2
要一次处理两个元素,请使用
index2
可能缺少数组。resize()。//这对我有用
Array.Resize<string[]>(ref strArrayList , count+ 1); // Count is the current length of strArrayList
Array.Resize(ref strArrayList,count+1);//Count是strArrayList的当前长度
标记了两个可能会遇到麻烦的地方:
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)
{
string[] strArray = str.Split(',');
strArrayList.Add(strArray);
}
}
for (int index1 = 0; index1 < strArrayList.Count; ++index1)
{
string[] strArray = strArrayList[index1];
// below that, what makes you believe strArray's length is >= 3 ?
Student student = new Student(strArray[0], strArray[1], strArray[2]);
int index2 = 3;
while (index2 < strArray.Length)
{
// here index2 will be < strArray.Length, but index2+1 might be ==
student.EnterGrade(int.Parse(strArray[index2]), int.Parse(strArray[index2 + 1]));
index2 += 2;
}
student.CalGrade();
theStudentList.Add(student);
}
}
catch (Exception e)
{
flag = true;
Console.WriteLine(e);
}
return flag;
}
如果
路径中的文件的最后一行为空行,则可能会出现问题。
您可以访问strArray[0]、strArray[1]、strArray[2]
,无论它是否为空数组。这很难诊断,因为您提供的信息有限。如果我能看到您在程序中运行的文件,作为路径输入,我就能够比较函数试图执行的操作和它试图执行操作的数据。这就是说,这看起来很熟悉,所以我会尝试解决它,至少给你一个工具来尝试
我在一个需要为图形程序读取CSV文件的程序中遇到了类似的问题。问题是,当CSV文件被创建时,一个空行被留在了en
for(int index2 = 3; index2 < strArray.Length - 1; index2 += 2)
{
student.EnterGrade(...);
}
public bool PopulateStudents(string path)
{
theStudentList = new List<Student>();
bool flag = false;
// EDIT: NEW VARIABLE int lineCounter declared and initialized before try block so it remains in scope when the catch block is called
int counter = 0;
try
{
List<string[]> strArrayList = new List<string[]>();
using (StreamReader streamReader = new StreamReader(path))
{
string str;
while ((str = streamReader.ReadLine()) != null)
{
string[] strArray = str.Split(',');
strArrayList.Add(strArray);
}
}
for (int index1 = 0; index1 < strArrayList.Count; ++index1)
{
// EDIT: UPDATE lineCounter
++lineCounter;
string[] strArray = strArrayList[index1];
Student student = new Student(strArray[0], strArray[1], strArray[2]);
int index2 = 3;
while (index2 < strArray.Length)
{
student.EnterGrade(int.Parse(strArray[index2]), int.Parse(strArray[index2 + 1]));
index2 += 2;
}
student.CalGrade();
theStudentList.Add(student);
}
}
catch (Exception e)
{
flag = true;
Console.WriteLine(e);
// EDIT: PRINT CURRENT LINE
Console.WriteLine(“error at line in file = “ + lineCounter);
}
return flag;
}