Solved

Java vector clone AGAIN with comp source

Posted on 1998-08-04
4
200 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
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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thymeleaf natural templating vs JSP 2 65
Java SE 8u111  Lot of stuff broke 11 52
xampp tool 12 21
MySQL  on Tomcat 8 22
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 second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

948 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

18 Experts available now in Live!

Get 1:1 Help Now