2013-10-02 82 views
-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; 
} 

}

+2

是什麼'numberOfCompetitors'? – fvrghl

+2

什麼是MAX_COMPETITORS? –

+0

爲什麼你需要遍歷數組?你沒有做任何事情。 – Farlan

回答

0

我敢打賭,你想一個新的Competitor添加到Competitor[]如果數組的長度大於numberOfCompetitors大。下面是這一種方法,而不需要通過數組迭代:

public boolean addCompetitor(Competitor competitor) { 
    boolean result = false; 
    if(numberOfCompetitors < competitors.length) { 
     result = true; 
     competitors[numberOfCompetitors++] = competitor; 
    } 
    return result; 
} 
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++){ 
+0

現在*這是我正在談論的。你的答案現在好多了。 +1。 :) – Makoto