数组列表故障



我正在做一个项目,我发现数组列表会更好,而不是使用数组
我知道我需要声明数组列表及其方法,但我不太确定该从哪里开始。有什么建议吗
这是代码。。。

  public class TestScoresModel {
    private ArrayList<Integer> tests;
    // private Student[] students;         // Array of students
    private int indexSelectedStudent;      // Position of current student
    private int studentCount;              // Current number of students
    public TestScoresModel() {
      // Initialize the data
      indexSelectedStudent = -1;
      studentCount = 0;
      // students = new Student[10];
      ArrayList list = new ArrayList();
    }
    // Mutator methods for adding and replacing students
    public String add(Student s) {
      if (studentCount == .length)
        return "SORRY: student list is full";
      else {
        students[studentCount] = s;
        indexSelectedStudent = studentCount;
        studentCount++;
        return null;
      }
    }
    public String replace(Student s){
      if (indexSelectedStudent == -1)
       return "Must add a student first";
      else {     
        students[indexSelectedStudent] = s;
        return null;
      }
    }
    // Navigation methods
    public Student first() {
      Student s = null;
      if (studentCount == 0)
        indexSelectedStudent = -1;
      else {
        indexSelectedStudent = 0;
        s = students[indexSelectedStudent];
      }
      return s;
    }
    public Student previous() {
      Student s = null;
      if (studentCount == 0)
       indexSelectedStudent = -1;
      else {
        indexSelectedStudent = Math.max (0, indexSelectedStudent - 1);
        s = students[indexSelectedStudent];
      }
      return s;
    }
    public Student next() {
      Student s = null;
      if (studentCount == 0)
        indexSelectedStudent = -1;
      else {
        indexSelectedStudent = Math.min (studentCount - 1, indexSelectedStudent + 1);
        s = students[indexSelectedStudent];
      }
      return s;
    }
    public Student last(){
      Student s = null;
      if (studentCount == 0)
        indexSelectedStudent = -1;
      else {
        indexSelectedStudent = studentCount - 1;
        s = students[indexSelectedStudent];
      }
      return s;
    }
    // Accessors to observe data
    public Student currentStudent() {
      if (indexSelectedStudent == -1)
        return null;
      else
        return students[indexSelectedStudent];
    }
    public int size() {
      return studentCount;
    }
    public int currentPosition() {
      return indexSelectedStudent;
    }
    public int getClassAverage(){
      if (studentCount == 0)
        return 0;
      int sum = 0;
      for (int i = 0; i < studentCount; i++)
        sum += students[i].getAverage();
      return sum / studentCount;
    }
    public Student getHighScore() {
      if (studentCount == 0)
        return null;
      else {
        Student s = students[0];
        for (int i = 1; i < studentCount; i++)
          if (s.getHighScore() < students[i].getHighScore())
            s = students[i];
        return s;
      }
    }
    public String toString() {
      String result = "";
        for (int i = 0; i < studentCount; i++)
          result = result + students[i] + "n";
        return result;
    }
}

arrayList可以包含任何类型的对象,为什么不把Student对象放在其中,然后根据需要访问它呢。

private ArrayList<Student> studentList = new ArrayList<Student>();

E.g将学生添加到列表

studentList.add(currentStudent);

a)您应该将其声明为List,因为您应该使用抽象(即接口),并且只有在实际构建时才使用ArrayList。

List<Integer> tests = new ArrayList<Integer>();

b) 使用已发布的List接口的JavaDocs作为参考。

下面是一个获取列表平均值的示例。

public double average(List<Integer> tests) {
  if (tests.isEmpty()) {
    // this return value depends on what you want to do when
    // there's no tests.
    return 0.0;
  }
  // you'll want to use a long here to help avoid overflow
  // since you could reach MAX_INT pretty easily with a large
  // list.
  long sum = 0L;
  for (Integer value : tests) {
    sum += value.longValue();
  }
  // you have to cast to double to allow it to do double arithmetic
  // and actually give you the decimal portion of the answer.
  return (double)sum / (double) tests.size();
}

最新更新