首页 > 解决方案 > 初学者字符串数组操作

问题描述

我的一项任务有问题。我完成了代码,并且必须提交作业,该作业将使用一堆测试对其进行自动评分。除了两个之外,我都通过了。我不知道测试的代码。我假设它来自这种方法:

/**
 * _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
}

我不确定抑制警告是针对什么的。是否有人在我的代码中看到任何可能影响上面显示的测试失败的错误?

标签: javaarraysstringintellij-idea

解决方案


问题出在这部分。

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 的值,而 your 的值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
}

现在 的值为j2 而 的值为i1 因此它将进入您的循环。然后,您将左索引复制到右索引。

也关于这个。

return 1000000;

这实际上是需要的,但您的回报必须是-1. 返回-1意味着该项目未插入,因为-1它不是数组中的有效索引。


推荐阅读