有人可以帮助我增加阵列的长度吗?

时间:2016-09-21 02:45:25

标签: java arrays junit

基本上,我需要创建一个比旧数组(PredatorList)大一个元素的新数组(newList)。我想从下面的代码中编辑的唯一内容是increaseArray方法中的内容。我不允许编辑方法名称/签名。我必须对它进行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++;  
}  

任何帮助都会很棒。干杯!

1 个答案:

答案 0 :(得分:1)

您创建了一个新列表,但之后不会将其分配给包含原始列表的字段。

this.PredatorList = newList;

缺失。