请参阅下面的代码,我使用枚举检索矢量元素和打印。使用枚举检索元素
package com.rajeev.enumeration;
import java.util.Enumeration;
import java.util.Vector;
public class Modification_On_Eumeration {
public static void main(String[] args) {
Vector<Integer> vector = new Vector<Integer>();
vector.add(1);
vector.add(2);
System.out.println(vector);//here it is printing 12(ok)
Enumeration<Integer> enumeration = vector.elements();
while (enumeration.hasMoreElements()) {
Integer integer = (Integer) enumeration.nextElement();
System.out.print(integer);//why it is printing 123 instead of 12(reason ????)
}
vector.add(3);
while (enumeration.hasMoreElements()) {
Integer integer1 = (Integer) enumeration.nextElement();
System.out.println(integer1);//why it is not printing ???
System.out.println("why not printing");
}
}
}
output
------
[1, 2]
123
why not printing
第一while循环是矢量的打印元件但第二while循环不打印元件,为什么呢?以及第一个while循环如何显示123而不是12?我正在学习java帮我..
在第二个while循环中,枚举打印'3'和'“为什么不打印''。 –