简体   繁体   中英

retriving elements using enumeration

see the below code,I am retriving elements from vector and printing using enumeration.

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

the first while loop is printing elements of vector but the second while loop is not printing elements,why? and how the first while loop is able display 123 instead of 12 ? I am learning java help me ..

You are using only one Eumeration object in both while loops.

The System.out.prinln(vector); line prints out [1, 2] .

The first while loop prints out 12 , because those are the only 2 elements at that time.

You add a third element, but you don't start over with a new Enumeration . The old Enumeration now sees another element, so it prints 3 , on the same line. Both loops contribute to the single line of output, 123 .

It also explains why you see

why not printing

printed only once.

If you modify your code to add some debug information (Which you should have done in the first place), you will see what is happening - everything is fine and working as expected:

    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("After adding 1 and 2 to the Vector<Integer>");
            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.println("in first loop before print");
                System.out.println(integer);//why it is printing 123 instead of 12(reason ????)
                System.out.println("in first loop after print");
            }
            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:

Executing the program....
$java -Xmx128M -Xms16M Modification_On_Eumeration

After adding 1 and 2 to the Vector<Integer>
[1, 2]
in first loop before print
1
in first loop after print
in first loop before print
2
in first loop after print
3
why not printing

actually the first while loop is printing 12 and the second while loop is printing (3 why not printing).you just modify your code like below .so that you can easily distinguish.

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 ????)
        }
        System.out.println();
        System.out.println("first loop finished");
        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");
        }
    }
}

The first while loop terminated because enumeration has ran out of elements. So there are no more elements for the next while-loop to iterate.

You need to create a new Enumeration that includes the new value. Refresh it with another call to

enumeration = vector.elements();

Enumeration s may only be used once. Once they run out, they must be re-created.

It should also be noted that using Vector and Enumeration are discouraged and should be replaced with ArrayList and Iterator .

From http://docs.oracle.com/javase/7/docs/api/java/util/Enumeration.html

The functionality of this interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration.

and http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html

As of the Java 2 platform v1.2, this class was retrofitted to implement the List interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Vector is synchronized. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.

You will need to create a new enumeration from the vector, because you already used the one you have for iteration. You cannot see the element 3 in the first loop simply because it wasn't added yet.

    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);
    enumeration = vector.elements();
    while (enumeration.hasMoreElements()) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM