1. public E remove(int index) {
    2. rangeCheck(index);
    3. modCount++;
    4. E oldValue = elementData(index);
    5. int numMoved = size - index - 1;
    6. if (numMoved > 0)
    7. System.arraycopy(elementData, index+1, elementData, index,
    8. numMoved);
    9. elementData[--size] = null; // clear to let GC do its work
    10. return oldValue;
    11. }
    12. 将指定下标后面一位到数组末尾的全部元素向前移动一个单位,并且把数组最后一个元素设置为null,这样方便之后将整个数组不再使用时,
    13. 会被GC,可以作为小技巧。而需要移动的元素个数为:size-index-1
    1. public boolean remove(Object o) { //只会删除集合中前面相同的元素
    2. if (o == null) {
    3. for (int index = 0; index < size; index++)
    4. if (elementData[index] == null) {
    5. fastRemove(index);
    6. return true;
    7. }
    8. } else {
    9. for (int index = 0; index < size; index++)
    10. if (o.equals(elementData[index])) {
    11. fastRemove(index);
    12. return true;
    13. }
    14. }
    15. return false;
    16. }
    1. private void fastRemove(int index) {
    2. modCount++;
    3. int numMoved = size - index - 1;
    4. if (numMoved > 0)
    5. System.arraycopy(elementData, index+1, elementData, index,
    6. numMoved);
    7. elementData[--size] = null; // clear to let GC do its work
    8. }