-2
基本上,我需要創建一個新的數組(newList)比舊數組(PredatorList)大一個元素。我想從下面的代碼編輯的唯一東西是增量數組方法中的東西。我不允許編輯方法名稱/簽名。我必須對其執行Junit測試,但是我一直收到一個錯誤,我不知道爲什麼。到目前爲止我的代碼:有人可以幫我增加一個數組的長度嗎?
public class Pack {
/**
* Predator list. This contains the list of all Predators.
* The list should never contain a null in the middle and should never have more than one
* blank at the end (eg a null).
*/
private Predator[] PredatorList = new Predator[0];
/**
* Increase the array by one. You will need to create a new array one element
* bigger than the old array.
* No External Classes Permitted to Be Used in This Method
* My Solution Length in Lines (note yours can be longer or shorter): 3
*
*/
private void increaseArray() {
int increment = 1;
Predator[] newList = new Predator[PredatorList.length + increment];
for (int i = 0; i < PredatorList.length; i++) {
newList[i] = PredatorList[i];
}
}
}
我有一個PackTest類,顯示這一點,但我真的不知道如何讀它。
public void testAddPredator2()
{
Pack list = null;
//add normal Predator
list = buildTestSet();
//System.out.println("->"+list.getNumberOfPredators());
int before = list.getNumberOfPredators();
list.addPredator(new Predator("Pony",100,100,5));
int after = list.getNumberOfPredators();
//System.out.println("->"+list.getNumberOfPredators());
assertEquals(before+1,after);
countOfSuccesfulTests++;
}
任何幫助將是偉大的。乾杯!
你得到什麼錯誤?我發現你需要在increaseArray的末尾添加'PredatorList = newList;'來將你的新列表分配給實例變量。 – iNan
你知道,我認爲每當我看到一個SO帖子說「我收到一個錯誤」時,我會迴應「我給你一個答案」。如果海報不會告訴我們錯誤是什麼,我不需要告訴他們答案是什麼。 :) – ajb
問題是,我不確定錯誤是什麼,因爲我無法感受Junit的工作方式。有一個PackTest類顯示...(我把它放在上面的原始問題中) – BobSacamano