0

我在反编译器中通过 Array List 类发现了这个方法..

private void fastRemove(int paramInt)
  {
    this.modCount += 1;
    int i = this.size - paramInt - 1;
    if (i > 0)
      System.arraycopy(this.elementData, paramInt + 1, this.elementData, paramInt, i);
    this.elementData[(--this.size)] = null;
  }

我只是想知道我们在什么情况下真正需要fastRemove()方法,请提供示例以便理解

在此处输入图像描述

4

3 回答 3

2

用户永远不会直接调用该方法(因此使用关键字private)。是调用fastRemove()时实际执行的删除操作。remove(Object o)

于 2012-09-02T16:56:17.597 回答
2

从此方法的评论中:

跳过边界检查且不返回已删除值的私有删除方法。

正如您可能已经看到的,这是由public remove()方法内部调用的。如果您查看此方法的源代码,您可以清楚地理解何时以及为何fastRemove()调用此方法:

public boolean remove(Object o) {
    if (o == null) {
      for (int index = 0; index < size; index++)
        if (elementData[index] == null) {
            fastRemove(index);
            return true;
        }
    } else {
        for (int index = 0; index < size; index++)
        if (o.equals(elementData[index])) {
            fastRemove(index);
            return true;
        }
        }
    return false;
}

使用此方法的想法很简单:不执行任何边界检查并在内部重新排列数组。

于 2012-09-02T16:57:23.690 回答
0

fastRemove(...)ArrayList是在类中使用的私有方法,由类中的方法使用remove。作为fastRemove私有方法,您不能使用它。但是,您可以使用remove使用此方法的方法。从remove方法的总结: -

Removes the first occurrence of the specified element from this list,
if it is present.  If the list does not contain the element, it is
unchanged.
于 2012-09-02T16:53:30.187 回答