当前位置:编程学习 > JAVA >>

arraylist中的fastremove方法怎么写

arraylist中的fastremove方法怎么写 --------------------编程问答--------------------
就是用代码 实习API 中arraylist 中的 fastremove 方法

--------------------编程问答-------------------- 看懂代码自己写啊 --------------------编程问答-------------------- arrayList 中  没有fastremove方法吧 --------------------编程问答-------------------- API中确实没有fastremove这个方法
但是翻了下arraylist源码 确实有 但是是private
就是只给ArrayList这个类用了
一般List list = new ArrayList();这种方法是不能调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;
    }

private void fastRemove(int index) {
        modCount++;
        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // Let gc do its work
    }

这个是源码 但是没什么研究的必要 因为这个和remove(int index)
唯一的区别就是remove(int index)新new了个list作为返回值
--------------------编程问答--------------------
 public E remove(int index) {
RangeCheck(index);

modCount++;
E oldValue = (E) elementData[index];

int numMoved = size - index - 1;
if (numMoved > 0)
    System.arraycopy(elementData, index+1, elementData, index,
     numMoved);
elementData[--size] = null; // Let gc do its work

return oldValue;
    }
--------------------编程问答-------------------- 我了个去,  (剑神一笑), 你回复的 正是我想要的,  太感谢了, 佩服的五体投地啊 --------------------编程问答-------------------- LZ提的啥问题嘛,要源码自己去jdk下拿呀。 --------------------编程问答-------------------- ArrayList没这个方法吧
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,