- 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());
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");
- }
- };
- }
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;
- }
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的时候,需要特别注意下
分享到:
相关推荐
经我细心探究,终于把J2ME中的Enumeration排序问题给解决了
USB枚举过程 USB Enumeration
NULL 博文链接:https://chaoyi.iteye.com/blog/2085158
Access-based Enumeration Windows Server 2003安装包
java枚举类型详细讲解,自己撰写的一本电子书,编写得很详细
cwe_latest 2021 common weakness enumeration.pdf
Network Enumeration .To keep me from implementing such a task over and over again枚举网络资源
Enumeration和Iterator接口功能相似,而且Iterator的功能还比Enumeration多,那么为什么还要使用Enumeration
Java 实例 - 使用 Enumeration 遍历 HashTable源代码+详细指导教程.zip
Windows.Devices.Enumeration
Iterator与Enumeration分别是实现迭代器和枚举器类的接口,下面就带大家来详解Java中的迭代迭代器Iterator与枚举器Enumeration,以及它们之间的区别.
请注意,从2.10版本开始,您可以使用-S标志将脚本提供给其他主机! linux-smart-enumeration 用于渗透测试和CTFLinux枚举工具 该项目的灵感来自并使用了许多测试。 不像LINENUM, lse尝试gradualy揭露根据其重要...
linux-smart-enumeration-master.zip
下面小编就为大家带来一篇详谈Enumeration接口和Iterator接口的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
swift代码-class类和Enumeration枚举的使用
CP2102驱动,CP2102具有集成度高的特点,可内置USB2.0全速功能控制器、USB收发器、晶体振荡器、EEPROM及异步串行数据总线(UART),支持调制解调器全功能信号,无需任何外部的USB器件。
Windows枚举 Powershell特权升级枚举脚本。 用法 ...这将需要一些时间,因此请耐心等待。 .\WindowsEnum.ps1 extended powershell -nologo -executionpolicy bypass -file WindowsEnum.ps1 extended