如何在c#中填充数组列表?
我的作业是阅读学生记录文件,然后计算并显示每门课程的最终成绩。作为一个老程序程序员,我很难弄清楚“适当”(例如,大多数OO)的程序流,因为我们应该从UML开始。一个大问题是,这是我第一次实现数组列表,即使在阅读了Java教程和一系列在线示例之后,我仍然对实际示例有点模糊。我认为学期末的大脑模糊使我无法将这些例子应用到这项作业中 到目前为止,我的情况如下: 我的主要方法包括如何在c#中填充数组列表?,c#,arrays,C#,Arrays,我的作业是阅读学生记录文件,然后计算并显示每门课程的最终成绩。作为一个老程序程序员,我很难弄清楚“适当”(例如,大多数OO)的程序流,因为我们应该从UML开始。一个大问题是,这是我第一次实现数组列表,即使在阅读了Java教程和一系列在线示例之后,我仍然对实际示例有点模糊。我认为学期末的大脑模糊使我无法将这些例子应用到这项作业中 到目前为止,我的情况如下: 我的主要方法包括 FinalStudentGrade finalGradeReport = new FinalStudentGrade();
FinalStudentGrade finalGradeReport = new FinalStudentGrade();
finalGradeReport.MainMethod();
调用Students.PopulateStudents(@“grades.txt”)
以显示成绩报告PopulateStudents()
读取数据文件,通过构造函数并基于以下数据成员创建学生对象:
string nameFirst;
string nameLast;
string studentID;
ArrayList Earned;
ArrayList Possible;
float average;
string letterGrade;
PopulateStudents
方法读取文件,为文件中的每一行创建一个学生对象,包括计算的平均成绩和字母分数。然后,需要将这些学生中的每一位添加到学生列表的列表中代码>
class Students
{
List<Student> theStudentList;
public bool PopulateStudents(string @sourceData)
{
String sourcePath = sourceData;
theStudentList = new List<Student>();
bool success = true;
try
{
StreamReader inputReader = new StreamReader(sourcePath);
while (inputReader != null)
{
String inputLine = inputReader.ReadLine();
char delim = ',';
String[] inputValues = inputLine.Split(delim);
班级学生
{
列出学生名单;
公共bool PopulateStudents(string@sourceData)
{
字符串sourcePath=sourceData;
学生列表=新列表();
布尔成功=真;
尝试
{
StreamReader inputReader=新的StreamReader(sourcePath);
while(inputReader!=null)
{
字符串inputLine=inputReader.ReadLine();
char delim=',';
字符串[]inputValues=inputLine.Split(delim);
…我被困住了
我正要定义nameFirst=inputValues[1];
,nameLast=inputValues[2];
,studentID=inputValues[0];
等,但后来我意识到我没有使用构造函数来创建Student对象
是否需要创建Student.nameFirst=…
,然后用这些对象填充studentlist
?进行此操作的最佳方式是什么?我是否以良好的方式可视化类之间的程序流?好吧,我没有看到定义构造函数,因此我假设您没有定义一个使用参数的构造函数。如果是这样,您可以先构造对象,然后设置属性:
Student s = new Student();
s.nameFirst = inputValues[1];
s.nameLast = inputValues[2];
s.studentID = inputValues[0];
或者使用初始化语法
Student s = new Student() {
nameFirst = inputValues[1]
nameLast = inputValues[2]
studentID = inputValues[0]
};
如果您确实定义了一个构造函数(例如,使用您提到的三个属性的构造函数),那么只需使用构造函数:
Student s = new Student(inputValues[1], inputValues[2], inputValues[0]);
然后只需添加创建的学生:
theStudentList.Add(s);
嗯,我没有看到定义构造函数,所以我假设您没有定义一个使用参数的构造函数。如果是这种情况,您可以先构造对象,然后设置属性:
Student s = new Student();
s.nameFirst = inputValues[1];
s.nameLast = inputValues[2];
s.studentID = inputValues[0];
或者使用初始化语法
Student s = new Student() {
nameFirst = inputValues[1]
nameLast = inputValues[2]
studentID = inputValues[0]
};
如果您确实定义了一个构造函数(例如,使用您提到的三个属性的构造函数),那么只需使用构造函数:
Student s = new Student(inputValues[1], inputValues[2], inputValues[0]);
然后只需添加创建的学生:
theStudentList.Add(s);
只是一个小小的附录,让它比D Stanley的答案更容易阅读:
theStudentList.Add(new Student {
nameFirst = inputValues[1],
nameLast = inputValues[2],
studentID = inputValues[0]
});
只是一个小小的附录,让它比D Stanley的答案更容易阅读:
theStudentList.Add(new Student {
nameFirst = inputValues[1],
nameLast = inputValues[2],
studentID = inputValues[0]
});
我没有看到任何代码创建
Student
对象添加到studentlist
@DStanley的答案是您需要的-作为循环的一部分。我没有看到任何代码创建Student
对象添加到studentlist
@DStanley的答案是您需要的-作为循环的一部分。+1表示初始化syntax.OP只需要确保他创建了一个默认构造函数。不,他没有-除非定义了不同的构造函数,否则默认情况下无参数构造函数是可用的。公平地说,如果定义了构造函数,我添加了一个选项。我假设他已经创建了一个构造函数,他在原始帖子中提到了一个构造函数。我是u如果你已经为你的类定义了一个构造函数,那么你需要创建一个默认的构造函数来使用对象初始化语法。好吧……那一切都是有意义的。我确实已经定义了一个构造函数。我假设我在while(inputReader!=null)中实例化了new Student()
loop和studentlist.Add
就在的右大括号之前,而?这对program flow有意义吗?@dwwilson66-学生列表
被定义为类的字段成员,因此您可以在该类的任何方法中引用它。您需要在循环之外构造它,否则每次迭代都会将它吹走+1用于初始化语法。OP只需要确保他创建了一个默认构造函数。不,他没有-除非定义了不同的构造函数,否则默认情况下无参数构造函数将可用。公平地说,如果定义了构造函数,我添加了一个选项。我假设他已经创建了一个构造函数,他在我的印象是,如果你已经为你的类定义了一个构造函数,那么你需要创建一个默认的构造函数来使用对象初始化语法。好吧……那一切都是有意义的。我确实已经定义了一个构造函数。我假设我实例化了new Student()
,就在里面(inputReader!=null)
循环,并学生列表。添加
就在的右大括号之前,而?这对program flow有意义吗?@dwwilson66-学生列表
被定义为类的字段成员,因此您可以在该类的任何方法中引用它。您需要在循环之外构造它,否则每次迭代都会将它吹走。