2017-02-20 121 views
-6

這是hw,我真的被困在如何讓我的代碼返回我想要它返回的東西。我試圖返回一個給定的索引值的字符串值。我認爲我所要做的只是返回給定索引處的字符串值,但我沒有得到正確的答案。返回與參數不同的類型

public void add(String candidate){ 
    if (candidate.equals(null)){ 
     throw new RuntimeException(); 
    } 
    String[] contenders = new String[candidates.length+1]; 

    // copy the array manually because I'm restricted from ArrayLists 
    for (int i = 0; i < candidates.length; i++){ 
     contenders[i] = this.candidates[i]; 
    } 
    this.candidate = candidate; 
    contenders[contenders.length-1] = this.candidate; 
    this.candidates = new String [contenders.length]; 

增加值到新建成的陣列後,測試者要在給定指標

public String get(int index){ 
    if (index < 0 || index > candidates.length) { 
     throw new RuntimeException("Your argument was not within bounds."); 
    } 
    for (int i = index; i < candidate.length(); i++){ 
     candidate = candidates[index]; 
    } 
    return candidate; 

我也一直在努力,我終於能夠有候選人停止指向獲取字符串值爲null,它給出了給定索引的錯誤值,所以例如我想'X'在候選人[3],但我得到'Y',因爲這是候選人保留的最後一個值。我試過只是返回候選人[索引],但它告訴我,該指數的值爲空。正如我已經通過調試器看起來,我的原始數組沒有被正確複製,但我不知道我應該接下來嘗試什麼。提前致謝。

這是我的構造函數:

public CandidateList(){ 
    candidates = new String[0]; 
} 

public CandidateList(String[] candidates){ 
    this.candidates = new String[candidates.length]; 
    CandidateList candidateList = new CandidateList(); 
+3

你有更大的問題..代碼是凌亂和有點沒有意義 –

+0

呃......什麼?您的代碼將始終返回數組中的最後一個值。爲什麼你需要一個for循環?循環直到'i Moira

+0

你是怎麼調用get(index)的。另外你是什麼意思原始數組不被正確複製?什麼是複製,什麼不是? – leoOrion

回答

1

有很多,可以在你的代碼加以改進,讓我補充一些意見

public void add(String candidate){ 
    //if candidate is actually null you are calling null.equals 
    //which means this will always result in a NullPointerException 
    //you can remove this if if you want 
    if (candidate.equals(null)){ 
     throw new RuntimeException(); 
    } 

    ... 

    //think about what you are doing here, 
    //you are setting this.candidates to a new empty array 
    //(is big contenders.length, but still empty) 
    this.candidates = new String [contenders.length]; 

第二部分:

public String get(int index){ 
    //you are missing an '=' in index >= candidates.length 
    if (index < 0 || index > candidates.length) { 
     throw new RuntimeException("Your argument was not within bounds."); 
    } 
    //this for loop is wrong, you are changing 'i' but never use it.. 
    //just return candidates[index] like you said before. 
    //It was probably null because of the error above 
    for (int i = index; i < candidate.length(); i++){ 
     candidate = candidates[index]; 
    } 
    return candidate; 

關於RuntimeException(RE)的說明:如果您遇到NullPointerException(NPE)並拋出一個RE,實際上您正在丟失信息n(因爲NPE是一個更具體的錯誤而不是RE)。如果你想趕上/至少扔放象「的候選人不能爲空」

現在讓我們來分析構造函數顯著消息:

public CandidateList(){ 
    candidates = new String[0]; 
} 

public CandidateList(String[] candidates){ 

    // you are doing the same error as above here: 
    // when you do this you create an EMPTY list of size candidates.lenght 
    // correct code is this.candidates = candidates 
    this.candidates = new String[candidates.length]; 

    // this is not necessary, constructors don't need to return anything, 
    //here you are just creating a new instance that will not be used anywhere 
    CandidateList candidateList = new CandidateList(); 

構造函數創建對象,它們不返回數據。我建議你看看這個問題Does a Java constructor return the Object reference?和一般閱讀更多有關構造函數

+0

我是Java的新手,它並不是很明顯,我沒有遵循控制流程。所以在我的junit測試一個測試稱爲我的構造函數,並給它一個字符串數組。然後,測試使用add方法將新值添加到數組的末尾。然後get方法應該返回新候選人所在索引處的單個字符串值。所以我想知道是否發送給構造函數的參數沒有被髮送到String []。 – jamin16

+0

謝謝你走過這個過程,而不是關注我的代碼有多混亂 – jamin16

+0

不用擔心,每個人都需要從某處開始:)你可以發佈你的構造函數嗎?如果你的junit失敗了,那麼也有這個代碼。否則,請將答案標記爲已接受 – deathyr

相關問題