数组列表的问题

数组列表的问题

问题描述:

我正在做一个项目,而不是使用数组,我认为数组列表会更好。我知道我需要声明数组列表及其方法,但我不太确定从哪里去。有什么建议么?这里的代码...数组列表的问题

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

我认为这可能是最好使用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

我想出一个数组列表会更好

可能。也许不会。这取决于。它看起来像使用基于ArrayList API的优点吗?

如果你的“列表”从不改变大小,并且你不需要在其中找到东西,那么数组就是一样好。

我知道我需要声明数组列表和它的方法,但我不 太知道该去哪里从那里

您需要创建一个ArrayList的实例的引用。这就是这么简单

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

在类的声明。你不需要“声明它的方法”。当你有一个对象的引用时,你可以调用它的方法。

使用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了解更多信息。

您使用的所有操作都在ArrayList API中进行镜像。有一点值得注意的是,你不能声明基本类型的ArrayList,但是对于每种基本类型,都存在一个Object,它是primative的盒装版本。

INT的盒装版本是整数,所以你必须

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

从那里,你需要查找你需要以操纵该阵列使用的方法。例如,如果要数42添加到数组的结尾,你会说

myList.add(42); 

ArrayList的API位于here

我试图将数组更改为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; 
} 

}