数组列表问题

时间:2012-03-20 23:07:31

标签: java arrays arraylist

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

  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;
    }
}

2 个答案:

答案 0 :(得分:1)

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

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

将学生添加到列表中

studentList.add(currentStudent);

答案 1 :(得分:1)

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

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

b)使用已发布的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();
}