Java Vector -clone BUG?? or just me?

Ok I have a source vector and wish to "loop" through it (note source may be incomplete, but the priniple is there)
However due to the Enumeration we may only loop an object once ! To solve this I though clone would be good,
because then the src vector is never looped and thus always is valid. or ??

Vector src = new Vector()
src.addElement()  // add all elements of type (StringTokenizer)
Vector tmp = (Vector)src.clone()
Vector tmp2 = (Vector)src.clone()
while( (StringTokenizer)tmp.hasMoreElements() ) {}
while( (StringTokenizer)tmp2.hasMoreElements() )
 //  We will never enter here !!

Any ideas to why we do not enter the second loop ?? It should be 2 sperate objects and thus we should be allowed to loop then independantly ??
Who is Participating?
mjenkinsConnect With a Mentor Commented:
The reason that the code fragment never enters the second loop is that you neglected to call nextElement() which means that you are looping forever on the first element. If you are concerned about the structure of the Vector changing while you iterate through it, by all means follow your original idea.

Just remember to use the Enumeration properly this time. It doesn't iterate unless you call nextElement().

Vector src = new Vector()
   // add all elements of type(StringTokenizer)
    Vector tmp = (Vector)src.clone();
    Vector tmp2 = (Vector)src.clone();
    Enumeration enum = tmp.elements();
    while( enum.hasMoreElements() )

    enum = tmp2.elements();
    while( enum.hasMoreElements() )

BTW: Ignore the advice that is telling you to use a for loop in place of the Enumeration. It would not only be bad style, it would be "bad OO"
Why do you want to use Enumeration?  You can use a simple for-loop like this:

Vector src = new Vector();

for(int i=0; i<src.size(); i++) {
    StringTokenizer stk = (StringTokenizer)src.elementAt(i);
    // Do whatever you want with this tokenizer
}   // Go to the next one...

You can performed this loop as many times as you want...
I must agree with shchuka that enumeration may be overcomplicated for what you want. It would be helpful if you explained why you think you need enumeration. Also, assuming you do, the critical bit as to why it does not enter the second loop will lie in how the enumeration object is acquired. In fact, as the quoted code stands, it doesn't look like it should even compile. tmp and tmp2 are Vector objects, and the Vector class does not provide a hasMoreElements method. To do enumeration you must obtain an enumeration object.

Either you in fact have a compile error, or the code was posted in error. Please illuminate your question with some additional information.

CrypToniCAuthor Commented:
Hahaha ooops! I guess I was alittle too tired! I should have seen that, I used more than 1 hour without solving that bug Thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.