?
Solved

Java vector clone AGAIN with comp source

Posted on 1998-08-04
4
Medium Priority
?
206 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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
Course of the Month11 days, 19 hours left to enroll

752 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