-2

我在编写此实例方法时遇到问题。我正在尝试编写该方法,以便该方法将检查是否有其他竞争对手的空间。如果有,另一个竞争对手将被添加到下一个可用的位置。

    public boolean addCompetitor(Competitor competitor) {
    // TODO
    for(int i=0;i<competitors.length; i++){
        if(numberOfCompetitors < MAX_COMPETITORS){
            numberOfCompetitors++;
            return true;
        }
    }

    return false;
}

我已经完成了一个循环,看看如果条件满足,我是否可以将变量添加到数组中。

这是完整的错误输出。

java.lang.NullPointerException
at Race.finishRace(Race.java:71)
at TestA2Classes.start(TestA2Classes.java:46)
at TestA2.main(TestA2.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

完整代码:

public class Race {
public static final String[] RACE_DESCRIPTIONS = {"Sprint", "Distance", "Eliminator", "Keirin"};
public static final int SPRINT = 0;
public static final int DISTANCE = 1;
public static final int ELIMINATOR = 2;
public static final int KEIRIN = 3;
public static final int MAX_COMPETITORS = 8;

private int number;
private int typeIndex;
private MyDate date;
private boolean hasFinished;
private Competitor[] competitors;
private int numberOfCompetitors;

public Race(int number, int typeIndex, MyDate date) { 
    // TODO
    this.number = number;
    this.typeIndex = typeIndex;
    this.date = date;
    this.hasFinished = false;
    this.numberOfCompetitors = 0;
    this.competitors = new Competitor[MAX_COMPETITORS];



}


public int getNumber() {
    // TODO
    return number;
}

public boolean getHasFinished() {
    // TODO
    return hasFinished;
}

public int getTypeIndex() {
    // TODO
    return typeIndex;
}

public MyDate getDate() {
    // TODO
    return date;
}

public Competitor getCompetitor(int number) {
    // TODO
    for(int i=0; i<competitors.length; i++){
        if(competitors[i].getNumber() == number){
            return competitors[i];
        }
    }
    return null;
}


public void finishRace(int first, int second, int third) { 
    // TODO
    this.hasFinished = true;
    for(int i=0; i<competitors.length; i++){
        if(competitors[i].getNumber() == first){
            competitors[i].setPosition(1);
        } else if(competitors[i].getNumber() == second){
            competitors[i].setPosition(2);
        } else if(competitors[i].getNumber() == third){
            competitors[i].setPosition(3);
        } else{
            competitors[i].setPosition(0);
        }
    }
}

public boolean addCompetitor(Competitor competitor) {
    // TODO
    if(numberOfCompetitors < MAX_COMPETITORS){
        competitors[numberOfCompetitors] = competitor;
        numberOfCompetitors++;
        return true;
    }

    return false;
}

public String toString() {
    // TODO
    String details = number + ", " + RACE_DESCRIPTIONS[typeIndex] + " [" + date + "]";
    if(!hasFinished){
        details += ": Race not finished";
    } else if(hasFinished){
        details += "\n     1st: " + competitors[0].getName();
        details += "\n     2nd: " + competitors[1].getName();
        details += "\n     3rd: " + competitors[2].getName();
    } else{
        details += "n/a";
    } 
    return details;
}

}

4

2 回答 2

3

就目前而言,如果你的条件在你的 for 循环中为假,那么它在所有迭代中都是假的,你只是在浪费计算时间。if 语句本身就足够了。

public boolean addCompetitor(Competitor competitor) {
    if(numberOfCompetitors < MAX_COMPETITORS)
    {
        competitors[numberOfCompetitors++] = competitor;
        return true;
    }
    return false;
}

您知道到目前为止您已经进入了多少竞争对手。您不需要循环,只需检查您是否直接添加了太多。

如果您还没有像这样保存添加了多少竞争对手,则需要循环(这要求任何合法条目都不能为空,否则将被覆盖):

public boolean addCompetitor(Competitor competitor)
{
    for(int i = 0 ; i < competitors.length ; i++)
    {
        if(competitors[i]==null)
        {
            competitors[i] = competitor;
            return true;
        }
    }
    return false;
}

您添加的竞争对手越多,这种方法将花费更长的时间 (O(n)),而另一种方法总是花费相同的时间 (O(1))

解决新编辑的错误

for(int i=0; i<competitors.length; i++){

您正在遍历您的整个竞争对手阵列,其中可能还没有竞争对手。

尝试将其更改为

for(int i=0; i<numberOfCompetitors; i++){
于 2013-10-02T13:02:09.777 回答
0

我敢打赌,Competitor如果Competitor[]数组的长度大于numberOfCompetitors. 这是一种方法,无需遍历数组:

public boolean addCompetitor(Competitor competitor) {
    boolean result = false;
    if(numberOfCompetitors < competitors.length) {
        result = true;
        competitors[numberOfCompetitors++] = competitor;
    }
    return result;
}
于 2013-10-02T13:01:46.917 回答