我创建了两种方法将泛型数据插入到ArrayList中:
public void add(T element) throws SortingException{
if(element == null)
throw new SortingException("addElement: element can't be null");
int index = getIndexInsert(element);
(this.array).add(index, element);
}
private int getIndexInsert(T element){
int index = 0;
boolean cont = true;
T currEl = null;
while((index<(this.array).size())&&cont){
currEl = (this.array).get(index);
if((this.comparator).compare(element, currEl)<0)
cont = false;
else index++;
}//while
return index;
}
现在,如果我用String类型测试这些方法,我得到了这个错误:
testStringOrder(sorting.SortingTests): null
这是我的测试方法:
@Test
public void testStringOrder() throws SortingException {
String[] arrExpected = {"ciao","come","stai"};
String[] arrActual = new String[3];
arrayString.add("come");
arrayString.add("ciao");
arrayString.add("stai");
arrayString=arrayString.insertionSort(0);
for(int i=0;i<3;i++) {
/*DEBUG:*/ System.out.println(arrayString.get(i));
arrActual[i] = arrayString.get(i);
}
assertArrayEquals(arrExpected,arrActual);
}
一旦我尝试调用第一个添加,它就会停止工作。我不知道问题是否是String类型,因为我用两个Integer数组测试它并且它可以工作。 arrayString在这里声明:
private Sorting<Integer> arrayInteger;
private Sorting<String> arrayString;
@Before
public void createArrayOfInteger(){
arrayInteger = new Sorting<>(new IntegerComparator());
}
public void createArrayOfString(){
arrayString = new Sorting<>(new StringComparator());
}