Nishi said:
Hi:
I have 2 vectors of 10 elements each and I needed to make a single vector out of them. So I converted both the vectors to strings using the toString() method of vector class and then used the concat() method of the String class to concatenate the strings.
Now I need to convert the resultant string back to a new resultant vector. How can I do that?
Thanks.
Hi Nishi!
At first I thought you were someone who is learning Java after some experience
with Basic (or these days, VisualBasic). Maybe that's true, but it's clear from
your posts of the past three months that Tony Morris is right in referring you to
the tutorial. Better yet, buy and read one of the many Java primers out there.
Then, as someone suggested, post questions like these to:
comp.lang.java.help
You will get quicker answers and people will be quicker to notice the more
fundamental mistakes in your code or your questions. Actually, you never seem
to post your non-working code, so you should start doing just that.
In this case, you and some of the previous responders have missed the simplest
way to deal with your current need. You forgot that Vector objects can be expanded
by simply adding more elements. vectorName.addElement( Object element) Or,
using ArrayList (a very good idea for this): list.add( Object element)
The *correct* answer to your question appears in the following code. The *best*
answer, however, requires that you convert the Vector objects to ArrayList
objects and change the names of the method calls appropriately....
----------------------------------------------------------------------
import java.util.*;
class VectorAddTest {
Vector One = null;
Vector Two = null;
public static void main( String[] args ) {
new VectorAddTest().runTest();
}
public void runTest() {
One = new Vector( 10 );
Two = new Vector( 10 );
for( int i=0; i<10; i++ ) {
One.addElement( new Integer( i ) );
Two.addElement( new Integer( i + 10 ) );
}
showIntVectors( "start" );
System.out.println();
// add the two vectors
One.addAll( Two );
Two = null;
showIntVectors( "finish" );
}
private void showIntVectors( String s ) {
if( s.equals( "start" ) )
System.out.println("After initializing the two vectors: " );
else if ( s.equals( "finish" ) )
System.out.println("After adding the second to the first: " );
else
return;
System.out.print( "One: " );
for( int i=0; i<One.size(); i++ )
System.out.print( (Integer)One.elementAt(i) + " " );
System.out.println();
System.out.print( "Two: " );
if( Two == null ) {
System.out.println( "null.");
return;
}
for( int i=0; i<Two.size(); i++ )
System.out.print( (Integer)Two.elementAt(i) + " " );
System.out.println();
}
}
-----------------------------------------------------------------------
/* Output:
After initializing the two vectors:
One: 0 1 2 3 4 5 6 7 8 9
Two: 10 11 12 13 14 15 16 17 18 19
After adding the second to the first:
One: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Two: null.
*/
------------------------------------------------------------------
I guess concatenating the two Vectors string representations could
get you the relative memory addresses of each Vector object's elements, but
you will have much less trouble if you follow the API descriptions
provided to all of us, and write your code accordingly.
Regards, Tony Dahlman