public E remove(int index) {
# 检查remove源码是,发现 其中对index 的检查仅限于上溢出检查
# 没有显示的对下溢出进行检查?
rangeCheck(index);
modCount++;
# 在此处,elementData 是一个Object[] 对象实例,会自动对下溢出进行检查 !!!
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
return oldValue;
}
# 上溢出检查,抛出 IndexOutOfBoundsExceptions异常
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
java中Array.remove()方法,源码中不对负索引进行检查
最新推荐文章于 2023-11-23 23:04:57 发布