我正在做一個項目,而不是使用數組,我認爲數組列表會更好。我知道我需要聲明數組列表及其方法,但我不太確定從哪裏去。有什麼建議麼?這裏的代碼...數組列表的問題
public class Student {
private String name;
private int[] tests;
public Student() {
this("");
}
public Student(String nm) {
this(nm, 3);
}
public Student(String nm, int n) {
name = nm;
tests = new int[n];
for (int i = 0; i < tests.length; i++) {
tests[i] = 0;
}
}
public Student(String nm, int[] t) {
tests = new int[t.length];
}
public Student(Student s) {
this(s.name, s.tests);
}
public int getNumberOfTests() {
return tests.length;
}
public void setName(String nm) {
name = nm;
}
public String getName() {
return name;
}
public void setScore(int i, int score) {
tests[i - 1] = score;
}
public int getScore(int i) {
return tests[i - 1];
}
public int getAverage() {
int sum = 0;
for (int score : tests) {
sum += score;
}
return sum/tests.length;
}
public int getHighScore() {
int highScore = 0;
for (int score : tests) {
highScore = Math.max(highScore, score);
}
return highScore;
}
public String toString() {
String str = "Name: " + name + "\n";
for (int i = 0; i < tests.length; i++) {
str += "test " + (i + 1) + ": " + tests[i] + "\n";
}
str += "Average: " + getAverage();
return str;
}
public String validateData() {
if (name.equals("")) {
return "SORRY: name required";
}
for (int score : tests) {
if (score < 0 || score > 100) {
String str = "SORRY: must have " + 0 + " <= test score <= " + 100;
return str;
}
}
return null;
}
}
你有什麼問題?什麼是上下文?請正確縮進您的代碼。 – talnicolas 2012-03-20 19:41:57
如果您想使用ArrayList而不是int數組,請通過更改private int []測試開始;私有ArrayList測試;沒有?此外,使它更通用一些的類型可能會更好的編碼實踐,而不是強制類型爲ArrayList(使用List而不是ArrayList) – 2012-03-20 19:44:50
數組列表在哪裏?我只看到一個數組。 – 2012-03-20 19:44:59