2012-03-20 154 views
0

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

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

你有什麼問題?什麼是上下文?請正確縮進您的代碼。 – talnicolas 2012-03-20 19:41:57

+1

如果您想使用ArrayList而不是int數組,請通過更改private int []測試開始;私有ArrayList測試;沒有?此外,使它更通用一些的類型可能會更好的編碼實踐,而不是強制類型爲ArrayList(使用List而不是ArrayList) – 2012-03-20 19:44:50

+0

數組列表在哪裏?我只看到一個數組。 – 2012-03-20 19:44:59

回答

0

我認爲這可能是最好使用stl vector,而不是讓自己的陣列

+0

這不是'C++' – Lol4t0 2012-03-20 19:44:02

+0

在java中沒有這種東西;) – Adrian 2012-03-20 19:44:21

+0

公平地說,這個問題最初是標記爲C++。 – 2012-03-20 19:45:31

3

我想出一個數組列表會更好

可能。也許不會。這取決於。它看起來像使用基於ArrayList API的優點嗎?

如果你的「列表」從不改變大小,並且你不需要在其中找到東西,那麼數組就是一樣好。

我知道我需要聲明數組列表和它的方法,但我不 太知道該去哪裏從那裏

您需要創建一個ArrayList的實例的引用。這就是這麼簡單

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

在類的聲明。你不需要「聲明它的方法」。當你有一個對象的引用時,你可以調用它的方法。

0

使用ArrayList,你只需要聲明並創建實例:

// <SomeObject> tells Java what kind of things go in the ArrayList 
ArrayList<SomeObject> aDescriptiveNameHere = new ArrayList<SomeObject>(); 

// This is also valid, since an ArrayList is also a List 
List<SomeObject> list = new ArrayList<SomeObject>(); 

然後你可以用add()方法添加的東西:

// Please don't name your list "list" 
list.add(new Thing(1)); 
list.add(new Thing(2)); 

您可以通過索引得到的東西(如你會用someArray[index])作爲:

list.get(index); 
// For example 
Thing t = list.get(5); 

你可能也是als o需要size()

請參閱the JavaDocs瞭解更多信息。

0

您使用的所有操作都在ArrayList API中進行鏡像。有一點值得注意的是,你不能聲明基本類型的ArrayList,但是對於每種基本類型,都存在一個Object,它是primative的盒裝版本。

INT的盒裝版本是整數,所以你必須

ArrayList<Integer> myList = new ArrayList<Integer>(); 

從那裏,你需要查找你需要以操縱該陣列使用的方法。例如,如果要數42添加到數組的結尾,你會說

myList.add(42); 

ArrayList的API位於here

0

我試圖將數組更改爲arraylist。 回覆如果這不能正確編譯。

import java.util.ArrayList;

公共類學生{

private String name; 
// private int[] tests; 
private ArrayList<Integer> tests; 

public Student() { 
    this(""); 
} 

public Student(String nm) { 
    // this(nm, 3); 
    name = nm; 
} 

/* 
* 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 Student(String name2, ArrayList<Integer> tests2) { 
    name = name2; 
    tests = tests2; 
} 

public int getNumberOfTests() { 
    return tests.size(); 
} 

public void setName(String nm) { 
    name = nm; 
} 

public String getName() { 
    return name; 
} 

// public void setScore(int i, int score) { 
// tests[i - 1] = score; 
public void setScore(int score) { 

    tests.add(score); 

} 

public int getScore(int i) { 
    // return tests[i - 1]; 
    return tests.get(i - 1); 
} 

public int getAverage() { 
    int sum = 0; 
    for (int score : tests) { 
     sum += score; 
    } 
    // return sum/tests.length; 
    return sum/tests.size(); 
} 

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.size(); i++) { 
     str += "test " + (i + 1) + ": " + tests.get(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; 
} 

public ArrayList<Integer> getTests() { 
    return tests; 
} 

public void setTests(ArrayList<Integer> tests) { 
    this.tests = tests; 
} 

}