Java ArrayBag grab()和remove()方法,使用.grab()方法后如何删除字符串?

Java ArrayBag grab()和remove()方法,使用.grab()方法后如何删除字符串?,java,netbeans,methods,Java,Netbeans,Methods,假设我想向ArrayBag添加3个形容词,通过使用JavaDoc for ArrayBag中的每个方法,如果grab()方法从ArrayBag中随机抽取一个形容词并使用它,我应该如何在使用后一次删除一个形容词 /** * Accessor method to retrieve a random element from this ArrayBag and will * remove the grabbed element from the ArrayBag * * @return A r

假设我想向ArrayBag添加3个形容词,通过使用JavaDoc for ArrayBag中的每个方法,如果grab()方法从ArrayBag中随机抽取一个形容词并使用它,我应该如何在使用后一次删除一个形容词

/**
 * Accessor method to retrieve a random element from this ArrayBag and will
 * remove the grabbed element from the ArrayBag
 *
 * @return A randomly selected element from this ArrayBag
 * @throws java.lang.IllegalStateException Indicated that the ArrayBag is
 * empty
 */
public E grab() {
    int i;
    //E n;

    if (items == 0) {
        throw new IllegalStateException("ArrayBag size is empty");
    }

    i = (int) (Math.random() * items + 1);

   // n = elementArray[i - 1];

    //if (items != 0) {
    //    remove(n);
    //}

    return elementArray[i - 1];
} 


一旦我调用
形容词.grab()
我该如何删除使用过的随机字符串?非常感谢您的帮助。

答案在于在从ArrayBag中删除元素后更改ArrayBag的大小。所有这些都在grab()方法中


答案在于在grab()方法中从ArrayBag中移除元素后更改ArrayBag的大小

/**
 * Remove one specified element from this ArrayBag
 *
 * @param target The element to remove from this ArrayBag
 * @return True if the element was removed from this ArrayBag; false
 * otherwise
 */
public boolean remove(E target) {
    int i;

    if (target == null) {
        i = 0;

        while ((i < items) && (elementArray[i] != null)) {
            i++;
        }
    } else {
        i = 0;

        while ((i < items) && (!target.equals(elementArray[i]))) {
            i++;
        }
    }
    if (i == items) {
        return false;
    } else {
        items--;
        elementArray[i] = elementArray[items];
        elementArray[items] = null;
        return true;
    }
}
    printText(3, "adjectives");
    adjectives.ensureCapacity(3);
    adjective = input.nextLine();
    String[] arr = adjective.split(" ");
    for(String ss : arr) {
        adjectives.add(ss);
/**
 * Accessor method to retrieve a random element from this ArrayBag and will
 * remove the grabbed element from the ArrayBag
 *
 * @return A randomly selected element from this ArrayBag
 * @throws java.lang.IllegalStateException Indicated that the ArrayBag is
 * empty
 */
public E grab() {
    int i;
    E n;

    if (items == 0) {
        throw new IllegalStateException("ArrayBag size is empty");
    }

    i = (int) (Math.random() * items + 1);

    n = elementArray[i - 1];

    remove(n);

    trimToSize();

    return n
}