Java中的插入排序,无法添加第二项? 公共类OrderedArrayList{ /**这是一个类型为T的对象数组*/ 私有T[]数组; 私有int numItems=0; 私有int itemsRemoved=0; @抑制警告(“未选中”) /** *构造一个OrderedArrayList,其中包含10个空插槽。请注意 *正在创建泛型类型的数组。您将希望重用此数组 *插入()方法中的配方。您可能还需要对此进行调整 *如果您添加了其他实例变量,则构造函数将增加一点。 */ 公共OrderedArrayList(){ 数组=(T[])新的可比[10]; } @抑制警告(“未选中”) /** *_第1部分:实现此方法_ * *在OrderedArrayList中插入新项。此方法应确保 *列表可以容纳新项,并在以下情况下增加支持数组 *必要。如果备份阵列必须增长以容纳新项目,则 *应增加2倍。新项目应放置在已排序的 *使用插入排序进行排序。请注意,应放置新项目 **在*列表中已存在的任何其他等效项之后。 * *@返回放置项目的索引。 */ 公共整数插入(T项){ 如果(numItems==0){ 数组[0]=项; numItems++; 返回0; } if(numItems==array.length){ T[]newArray=(T[])新的可比较[array.length*2]; for(int i=0;i=0;j--){ if(array[j].compare to(item)

Java中的插入排序,无法添加第二项? 公共类OrderedArrayList{ /**这是一个类型为T的对象数组*/ 私有T[]数组; 私有int numItems=0; 私有int itemsRemoved=0; @抑制警告(“未选中”) /** *构造一个OrderedArrayList,其中包含10个空插槽。请注意 *正在创建泛型类型的数组。您将希望重用此数组 *插入()方法中的配方。您可能还需要对此进行调整 *如果您添加了其他实例变量,则构造函数将增加一点。 */ 公共OrderedArrayList(){ 数组=(T[])新的可比[10]; } @抑制警告(“未选中”) /** *_第1部分:实现此方法_ * *在OrderedArrayList中插入新项。此方法应确保 *列表可以容纳新项,并在以下情况下增加支持数组 *必要。如果备份阵列必须增长以容纳新项目,则 *应增加2倍。新项目应放置在已排序的 *使用插入排序进行排序。请注意,应放置新项目 **在*列表中已存在的任何其他等效项之后。 * *@返回放置项目的索引。 */ 公共整数插入(T项){ 如果(numItems==0){ 数组[0]=项; numItems++; 返回0; } if(numItems==array.length){ T[]newArray=(T[])新的可比较[array.length*2]; for(int i=0;i=0;j--){ if(array[j].compare to(item),java,Java,通常,数组中当前的项分布在array[0]和array[numItems-1]之间,包括其他数组值null。但是,紧接着numItems++for循环之前的for之后,数组中的项分布在数组[0]和数组[numItems-2]之间,因为新的数组[numItems-1]仍然包含增量之前的null值。因此,当到达后续的if语句时,array[j]的第一个值与array[numItems-1]相同,仍然是null,尝试在null上调用compareTo会导致NullPointerException,这就

通常,数组中当前的项分布在
array[0]
array[numItems-1]之间
,包括其他数组值
null
。但是,紧接着
numItems++
for循环之前的
for
之后,数组中的项分布在
数组[0]
数组[numItems-2]
之间,因为新的
数组[numItems-1]
仍然包含增量之前的
null
值。因此,当到达后续的
if
语句时,
array[j]
的第一个值与
array[numItems-1]相同
,仍然是
null
,尝试在
null
上调用
compareTo
会导致
NullPointerException
,这就是您看到的错误


我建议将
numItems++
语句移动到
for
循环之后。

“它说在insert Zeno中有一个错误”我的精神头盔今天似乎不起作用。也许你可以告诉我们(我指的是任何其他精神头盔坏了的人)错误是什么?你能告诉我error@RaviKumar我不知道该怎么做,抱歉,我对编程很陌生。如果你使用任何ide,那么你可以在控制台中看到异常,或者如果你通过命令行运行它,那么你可以从cmd复制异常跟踪。@RaviKumar它让我知道测试类和c的位置如果(数组[j].compareTo(项),则这些方法处于有问题的位置
public class OrderedArrayList<T extends Comparable<T>> {

/** This is an array of Objects of type T */
private T[] array;
private int numItems = 0;
private int itemsRemoved=0;

@SuppressWarnings("unchecked")
/**
 * Construct an OrderedArrayList with 10 empty slots. Note the recipe for 
 * creating an array with the generic type. You'll want to reuse this 
 * recipe in the insert() method.  You'll also possibly want to tweak this 
 * constructor a bit if you add other instance variables.
 */
public OrderedArrayList() {
    array = (T[]) new Comparable[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(T item) {
    if (numItems==0){
        array[0]=item;
        numItems++;
        return 0;
    }
    if (numItems==array.length){
        T[] newArray = (T[]) new Comparable[array.length * 2];
        for (int i = 0; i < array.length; i++){
            newArray[i] = array[i];
        }
        array=newArray;
    }
    numItems++;
    for (int j = numItems-1; j >= 0; j--) {

        if (array[j].compareTo(item) <= 0){
            array[j+1] = item;
            return j+1;
        }

        else{
            array[j+1] = array[j];

        }


    }
    return -1;
}
    @Test
public void removeWithTwoDifferentItems() {
    int i;
    OrderedArrayList<String> a = new OrderedArrayList<String>();
    a.insert("Hello!");
    a.insert("Zeno.");
    i = a.find("Hello!");
    assertEquals("Can't properly find the only string that should be in the list!", 0, i );
    i = a.remove("Hel"+"lo!");
    assertEquals("remove() expecting 1, but got something else", 1, i );
}