【问题标题】:Beginner String Array Manipulation初学者字符串数组操作
【发布时间】:2019-02-20 01:21:14
【问题描述】:

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

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

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

【问题讨论】:

    标签: java arrays string intellij-idea


    【解决方案1】:

    问题出在这部分。

    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 不是数组中的有效索引。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多