Solved

Java vector clone AGAIN with comp source

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
import as existing maven project 3 77
Find out all repos that a user is most active on Github 1 57
Java array sort 10 62
Java ArrayList and if statement 2 55
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

734 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