我无法完成一项任务。我完成了代码,必须提交作业,该作业将使用一堆测试对其进行自动评分。除了两个我都通过了。我不知道测试代码。我假设它来自此方法:
/**
* _Part 2: Implement this method._
*
* @return the number of items in the list.
*/
public int size() {
int size = 0; //int holder for size of elements in array
for (int i = 0; i < array.length; i++){ //loop to run through up to length of array
if(array[i] != null){ //if data at array index has a value, increase int size
size++;
}
else{ //if data at array index has no value, there are not anymore elements to check for
break;
}
}
return size; //returning the number of elements in the array
}
这些是我得到的错误:
Starting: insertTwoItems
Failed: insertTwoItems
Hint: Size should be 2 after two inserts! expected:<2> but was:<1>
Finished: insertTwoItems
Starting: insertThreeItems
Failed: insertThreeItems
Hint: Size should be 2 after two inserts! expected:<2> but was:<1>
Finished: insertThreeItems
构造函数很简单:array = new String [10];
这是我将新字符串插入数组的方式
@SuppressWarnings("unchecked")
/**
* _Part 1: Implement this method._
*
* Inserts a new item in the OrderedArrayList. This method should ensure
* that the list can hold the new item, and grow the backing array if
* necessary. If the backing array must grow to accommodate the new item, it
* should grow by a factor of 2. The new item should be placed in sorted
* order using insertion sort. Note that the new item should be placed
* *after* any other equivalent items that are already in the list.
*
* @return the index at which the item was placed.
*/
public int insert(String item) {
if(array.length <= size()){ //checking if string array is full
String[] tempArray = array; //creating a temp string array, to make this.array bigger
array = new String[array.length*2]; //doubling the length of array
for(int i = 0; i < tempArray.length; i++){ //putting tempArray values back into original array
array[i] = tempArray[i];
}
}
for(int i = 0; i < array.length; i++){ //loop to find location to add item into
if(array[i] == null){ //if array index location is null, then put item in that spot.
array[i] = item;
return i; //returning index location item was put at
}
if(item.compareTo(array[i]) < 0){ //will only insert until array index is alphabetically smaller than item
for(int j = size()-1; j > i; j--){ //moving all items from array size down to index j
array[j+1] = array[j]; //copying left index into right index
}
array[i] = item; //once all indexes from where item should go is moved to the right, index is put in the array
return i; //returning index of array where item went
}
}
return 1000000; //not necessary, but cannot compile without a return outside of loops. Should not reach this
}
我不确定抑制警告的含义是什么。有人看到我的代码中有任何错误可能会影响上述测试失败吗?
答案 0 :(得分:0)
问题出在这部分。
for(int j = size()-1; j > i; j--){ //moving all items from array size down to index j
array[j+1] = array[j]; //copying left index into right index
}
j
的分配不正确。例如,由于您有一个数组['a','z']
,并且您将要插入'c'
。 j
的值为1,而您的i
的值为1,因此不会进入循环。您应该将其更改为此。
for (int j = size(); j >= i; j--) { // moving all items from array size down to index j
array[j] = array[j - 1]; // copying left index into right index
}
现在j
的值将为2,而i
的值将为1,因此它将进入循环。然后,您将左索引复制到右索引。
与此有关。
return 1000000;
这实际上是必需的,但您的退货必须为-1
。返回-1
意味着未插入项目,因为-1
在数组中不是有效索引。