数组列表的麻烦

时间:2012-03-20 19:40:13

标签: java arraylist

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

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

5 个答案:

答案 0 :(得分:3)

  

我认为数组列表会更好

也许。也许不吧。这取决于。您是否会在使用ArrayList API?

的基础上获益?

如果您的“列表”永远不会改变大小,并且您不需要在其中找到任何内容,那么数组也一样好。

  

我知道我需要声明数组列表及其方法,但我不是   太确定从那里去哪里

您需要创建对ArrayList实例的引用。这就像

一样简单

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

在你的班级声明中。您不需要“声明其方法”。当您引用对象时,可以调用其方法。

答案 1 :(得分:0)

我认为使用stl vector代替制作自己的数组

会更好

答案 2 :(得分: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);

您可能还需要size()

有关详细信息,请参阅the JavaDocs

答案 3 :(得分:0)

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

盒装版本的int是Integer,所以你有

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

从那里,你需要查找你需要使用的方法来操作数组。例如,如果要将数字42添加到数组的末尾,则可以说

myList.add(42);

ArrayList API位于here

答案 4 :(得分: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;
}

}

相关问题