-1

我在尝试将计算结果插入学生结果列表的 ArrayList 时遇到问题。

它是这样的。

  1. ArrayList(称为 FullList)包含 Student 对象的列表。
  2. Student 对象具有名称(作为字符串)和结果列表(作为 ArrayList)。
  3. 当我收到一个 Result 对象时,它带有一个名称(作为字符串)和结果(作为整数)。
  4. 我想将此 Result 对象插入到 Student 对象的结果列表中。
  5. 但是,学生姓名不是预先定义的,而是在收到 Results 对象时推断出来的。
  6. 也就是说,如果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);

   }
}
4

2 回答 2

2

对于列表中的每个学生,如果该学生的名称与要插入的结果不同,则您将插入一个新学生:

for (Student s : fullList) {
     if (s.getName().equalsIgnoreCase(this.name)) { // current student has same name as result
         s.setResult(this);
     }
     else { // current student doesn't have the same name as result
        Student s = new Student(this.name); 
        s.setResult(this);
        fullList.add(s);
     }
}

上面应该写成:

Student s = findStudentWithName(fullList, this.name);
if (s == null) {
    Student s = new Student(this.name); 
    s.setResult(this);
    fullList.add(s);
}
else {
    s.setResult(this);
}

findStudentWithName 方法如下所示:

private Student findStudentWithName(List<Student> students, String name) {
    for (Student s : students) {
        if (s.getName().equalsIgnoreCase(name)) {
            return s;
        }
    }
    // no student found.
    return null;
}
于 2012-11-16T06:46:26.727 回答
2

Map/HashMap代替 List使用:

       Map<String, Student> fullStudents= new HashMap<Student>();

并将其用作:

    public void setResult(Map<String, Student> fullMap) {
            Student s = fullMap.get(this.name.toLowerCase());
            if(s == null){
              s = new Student(this.name);
              fullMap.put(this.name.toLowerCase(), s);
             }
             s.setResult(this);
     }

它更简单,更清洁。

另外要随时获取学生列表,您可以简单地执行以下操作:

       List<Student> fullList = fullMap.values();
于 2012-11-16T06:55:00.820 回答