Solved

Java vector clone AGAIN with comp source

Posted on 1998-08-04
4
196 Views
Last Modified: 2010-04-16
It seems I was moving to fast for my own good, look at this source

private Vector find_ref( String lng, String lat )  {
Vector v = new Vector();
Vector vv = (Vector)(CoordVect.clone());
 int i = 0;
 int stop = CoordVect.size();  // A vector with StringTokenizer( String, "," ) as arg to addElement()
int goodi = 0;
 do            // Find a good reference point
   
{
      while( ((StringTokenizer)(vv.elementAt(i))).hasMoreTokens() ) {
         fill = ((StringTokenizer)(vv.elementAt(i))).nextToken();   }
        // goodi is assigned a value in the range 0..i      
       i++;  
} while( i < stop );
Vector vec = (Vector)(CoordVect.clone());
while( ((StringTokenizer)(vec.elementAt(goodi))).hasMoreTokens() )    {
     fill = new String( ((StringTokenizer)(vec.elementAt(goodi))).nextToken());    } // This never happens
return v; // which is filled 1st ofcourse
  }

Can you explain why the second loop is never entered ?? Or is it as simple as that elementAt(i) shouldn't be used but instead nextElement() ???   (since I obviously used elementAt(i) I would like to keep it this way! Any input ??
0
Comment
Question by:CrypToniC
  • 2
4 Comments
 
LVL 5

Expert Comment

by:msmolyak
Comment Utility
A few questions and suggestions:

1. Why do you keep cloning the vectors. The good reason to clone the vector is if you want to modify one copy leaving the other intact. The other reason is for multithreaded access to the vector elements. If you have read-only access in a single thread there is no reason to clone.

2. Operation ((StringTokenizer)(vec.elementAt(goodi)) is pretty expensive timewise. If you are going to be repeatedly accessing the same element of the vector assign it to some variablle and then use it.

3. Put a few simple print statements after the first loop checking the size of the vector, the value of goodi index, the element of the vector at this index and if the element exists check whether it has any tokens before entering the second loop.  Your code here is incomplete so it is not immediately obvious what's going on.  Check the values an post them, we will go from there.
0
 
LVL 3

Expert Comment

by:mjenkins
Comment Utility
The answer to your question "Can you explain why the second loop is never entered ??" is simple - BTW. But you'll need to unlock the question first.

That not withstanding, you have a deeper problem. Answering your question (which again, is simple) will not solve your problem. You have a rather severe design flaw on your hands that you need to straighten out.

Could you please explain in a little more detail what it is that you are trying to do? What is the overall objective? Why are you storing the coordinates in Strings? Why are you storing StringTokenizers in the Vectors?


0
 
LVL 3

Accepted Solution

by:
mjenkins earned 100 total points
Comment Utility
The answer to your question is:

The Vector.clone() is making a shallow copy of the vector. In other words, you are copying the underlying array from the Vector but not the elements themselves. Both the clone and the original (and the second clone) all refer to the same elements. They are just stored in multiple arrays (vectors).

The StringTokenizer is a single-pass construct. Once you've cycled through it in the first loop by calling      

while( ((StringTokenizer)(vv.elementAt(i))).hasMoreTokens() ) {
             fill = ((StringTokenizer)(vv.elementAt(i))).nextToken();   }

the StringTokenizer is exhasted. So the second loop's call to
while( ((StringTokenizer)(vec.elementAt(goodi))).hasMoreTokens() )  
 always falls through -- hasMoreTokens() returns false for every goodi.

How to solve this? Instead of storing the StringTokenizer in the Vector, store the String and create the Tokenizer when you are ready to use it.

I still would like to know exactly what you are doing because I suspect that you could do what you want in a much simpler manner.

0
 
LVL 1

Author Comment

by:CrypToniC
Comment Utility
What I basicly do is read (alot) of data from a file (in format x,x,x,x,x,x,x,xx,x,\ny,y,y,y,y,y,)
and wish to use this data in a good way. So i suppose your solution (store strings) is the only one...I was aware of the exhasted stuff, however I thought the cloning would solve it for me! Well anyway thanks, now I have some code to re-write
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now