2012-03-20 39 views
-3

我正在做一個項目,而不是使用數組,我發現數組列表會更好。
我知道我需要聲明數組列表及其方法,但我不太確定從那裏去哪裏。有什麼建議麼?
這裏的代碼...數組列表問題

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

不知道你在問什麼。你的代碼看起來像仍然基於數組實現,你剛剛替換了數組的聲明。除了「請做這項工作」還有一個問題嗎? – 2012-03-20 23:13:35

+0

Ctrl + Shift + F,請....你的標籤遍佈各處。 – aroth 2012-03-20 23:45:09

回答

1

一個ArrayList可以包含任何類型的對象,所以你爲什麼不只是把你的學生對象,然後訪問它,只要你想。

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

如增加學生列出

studentList.add(currentStudent); 
1

一),你應該把它聲明爲列表,因爲你應該用抽象(即接口)工作,只有當你真正使用ArrayList構建。

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

b)使用發佈的JavaDocs作爲List接口作爲參考。

下面是獲取列表平均值的示例。

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