我在尝试将计算结果插入学生结果列表的 ArrayList 时遇到问题。
它是这样的。
- ArrayList(称为 FullList)包含 Student 对象的列表。
- Student 对象具有名称(作为字符串)和结果列表(作为 ArrayList)。
- 当我收到一个 Result 对象时,它带有一个名称(作为字符串)和结果(作为整数)。
- 我想将此 Result 对象插入到 Student 对象的结果列表中。
- 但是,学生姓名不是预先定义的,而是在收到 Results 对象时推断出来的。
- 也就是说,如果FullList中没有Student对象,Result对象会触发一个Student对象的创建,并将自己插入到Student对象的resultlist中。然后将创建的学生对象插入到 FullList 中。
我已经编写了下面的代码,但是我将同一标签多次插入到 FullList 中,而不是将多个结果插入到每个标签中。
Q1:我不知道出了什么问题!需要大师的心灵刺!Q2:我将为多达 90,000 名学生使用以下代码。这是容纳学生成绩的可行方式吗?
class Result {
String name;
int result;
public Result(String name, int result) {
this.name = name;
this.result = result;
}
/* start - Getting hungup over this method
* Something is wrong here and i don't know what it is
*/
public void setResult(ArrayList <Student> fullList) {
for (Student s : fullList) {
if (s.getName().equalsIgnoreCase(this.name)) {
s.setResult(this);
}
else { /* enters here if Student does not exist in fullList */
Student s = new Student(this.name); /* create new student */
s.setResult(this); /* insert result into Student's resultslist */
fullList.add(s); /* add Student into fullList */
}
}
}
/* end */
public String getName() {
return this.name;
}
}
class Student {
String name;
ArrayList <Result> resultslist;
public Student(String name) {
this.name = name;
resultslist = new ArrayList <Result> ();
}
public void setResult(Result result) {
this.resultslist.add(result);
}
public String getName() {
return this.name;
}
}
class StudentResults {
public static void main (String [] args) {
ArrayList <Student> FullList = new ArrayList <Student> ();
Result r1 = new Result("John", 12);
Result r2 = new Result("Jamie", 99);
Result r3 = new Result("John", 69);
Result r4 = new Result("Jacque", 56);
Result r5 = new Result("Jacque", 100);
Result r6 = new Result("Jamie", 100);
r1.setResult(FullList);
r2.setResult(FullList);
r3.setResult(FullList);
r4.setResult(FullList);
r5.setResult(FullList);
r6.setResult(FullList);
}
}