Enumeration 使用中一些经验


Vector v = new Vector();
for(int i=0;i<10;i++)
v.add(new Integer(i));

Enumeration e = v.elements();

while(e.hasMoreElements())
{
Integer i = (Integer)e.nextElement();
v.remove(i);

}

System.out.println(v.size());


如果没去执行这段程序,可能有的人会认为
System.out.println(v.size());
输出来的是0,但实际上是5,为什么会这样的呢?

在回答这个问题前,我们先看看2段代码

public Enumeration<E> elements() {
return new Enumeration<E>() {
int count = 0;

public boolean hasMoreElements() {
return count < elementCount;
}

public E nextElement() {
synchronized (Vector.this) {
if (count < elementCount) {
return (E)elementData[count++];
}
}
throw new NoSuchElementException("Vector Enumeration");
}
};
}


上面这段代码就是v.elements()代码


public synchronized void removeElementAt(int index) {
modCount++;
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
else if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
int j = elementCount - index - 1;
if (j > 0) {
System.arraycopy(elementData, index + 1, elementData, index, j);
}
elementCount--;
elementData[elementCount] = null; /* to let gc do its work */
}


上面这段代码,就是v.remove(Object o)的主核心代码

分析:
先看第一段代码:

从代码中,我们得知Enumenation.nextElement 会将指针下划到下一个元素

在看第二段代码:

从代码中,我们得知v.remove 进行操作时,是将后面的数组覆盖掉 所要删除元素的位置,最后一个则用null

现在我们理顺下思路吧,

v 里面存放的数是 0 1 2 3 4 5 6 7 8 9

首先e.nextElement() 指针所在位置就是 0,也就是数组的第一个索引

当我们进行v.reomve的时候,后面的数组覆盖了前面的数组

v.remove 处理完后: 1 2 3 4 5 6 7 8 9

这时候,进入下次循环

e.nextElement() 这时候的指针位置就是 2, 而漏过了 1 这个数据,

循环结束后,就漏掉了5个数,

所以,大家在做这样的remove的时候,需要特别注意下
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值