Convert a string to a vector?

N

Nishi Bhonsle

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.
 
C

Collin VanDyck

Nishi Bhonsle 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.

Instead of converting to Strings and back, can you not just do this? :

Vector vOne = new Vector(10);
Vector vTwo = new Vector(10);

// code to put things into these vectors.

Vector vThree = new Vector(20);
vThree.addAll(vOne);
vThree.addAll(vTwo);

If you were storing characters in the strings, try using a for loop to
iterator over the individual characters in each string:

for (int idx=0; idx < stringOne.length; idx++) {
Character ch = new Character(stringOne.charAt(idx));
// add this ch to your new vector?
}

Collin
 
A

Andrew Thompson

Nishi Bhonsle wrote:
.....
I have 2 vectors of 10 elements each and I needed to make a single
vector out of them.

Why? What is the actual input you have
and the output you require?

It depends on why you ever had vectors,
they use much more resources than arrays.
..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.

If you want to end up with 20 elements in
a new data vector or array, converting them
to strings in the meantime is a terribly bad idea.

A better way is to create a single array/vector
as big as both, then simply loop through
each and add the elements to the final data
vector/array.

_However_

You need to direct your question over to..
http://groups.google.com.au/groups?group=comp.lang.java.help
where these sorts of questions are answered.
 
T

Thomas Schodt

Nishi said:
I have 2 vectors of 10 elements each and I need to make a single vector out of them.

Vector implements Collection.
Vector.addAll() takes a Collection.
 
T

Tony Morris

Nishi Bhonsle 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.

a) you generally shouldn't be using java.util.Vector if you don't have to
support 1.1 - use java.util.ArrayList instead (and synchronize it if you
require thread safety)
b) suggestions that use a java.util.Vector reference make my stomach churn -
use a java.util.List reference instead.
c) if the object held by the List is a String, cast it rather than call the
toString() method.

http://java.sun.com/docs/books/tutorial/collections/intro/index.html

Good luck !

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
T

Tony Dahlman

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,260
Messages
2,571,039
Members
48,768
Latest member
first4landlord

Latest Threads

Top