[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Java vector clone AGAIN with comp source

Posted on 1998-08-04
4
Medium Priority
?
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Expert Comment

by:msmolyak
ID: 1231227
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
ID: 1231228
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 400 total points
ID: 1231229
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
ID: 1231230
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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.
Suggested Courses

650 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