Solved

Java Vector -clone BUG?? or just me?

Posted on 1998-08-04
4
319 Views
Last Modified: 2010-04-16
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 ??
0
Comment
Question by:CrypToniC
4 Comments
 
LVL 2

Expert Comment

by:shchuka
ID: 1230868
Why do you want to use Enumeration?  You can use a simple for-loop like this:

Vector src = new Vector();
src.addElement();
.
src.addElement();

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...
0
 
LVL 16

Expert Comment

by:imladris
ID: 1230869
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.

0
 
LVL 3

Accepted Solution

by:
mjenkins earned 100 total points
ID: 1230870
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)
    src.addElement()  
    Vector tmp = (Vector)src.clone();
    Vector tmp2 = (Vector)src.clone();
    Enumeration enum = tmp.elements();
    while( enum.hasMoreElements() )
    {
      (StringTokenizer)enum.nextElement();
    }

    enum = tmp2.elements();
    while( enum.hasMoreElements() )
    {
      (StringTokenizer)enum.nextElement();
    }

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"
0
 
LVL 1

Author Comment

by:CrypToniC
ID: 1230871
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 177
Unhandled exception type Exception 18 42
How do I remove an object from a 3 40
jsp error 6 26
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question