Converting vector to string array
How can I convert values in a vector into a string array?
Vector formsVector = new Vector();
while (rs.next())
{formsVector.add(rs.getString("forms"));}
String forms[] = (String[])formsVector.toArray();
I tried the above line but it did not work. Any input please?
Thanks
.... What is the difference between the two as
according to online help, both are same.
String forms[] = (String [])formsVector.toArray();
String forms[] = (String [])formsVector.toArray( new
String[0] );The difference lies in the type of the object returned from toArray. The first form you list, formsVector.toArray(), always returns an Object[]. In your example, you'll get a ClassCastException at runtime when you cast to String[].
The second form will try to use the array you pass in. If it's not big enough, it'll create a new one of the same type as that array. This is what's happening when passing a zero-length array into toArray.
My personal preference is to save an extra instantiation and build the array to the proper size to begin with:String forms[] = (String [])formsVector.toArray( new String[formsVector.size()] );
Similar Messages
-
Hello all...
How can i convert a vector to a string array?If it's inserted into the vector as a String, then the toArray() method is exactly what you want. Otherwise, a simple for loop doing a "array[i] = thing.toString()" bit ought to do nicely.
-
hi,
i have a vector containing Doubles which looks like this: [3.1, 4.9, 2.2, 7.7] and i want to convert it into a vector containing a string so it will look like this: [3.1 4.9 2.2 7.7]
any ideas?
thanks in advance,
alI wouldn't extend Vector and over-ride toString just to reformat the element's toString methods. In fact, I don't even think any thing is necessary, if all you want to do is dispaly the values of these strings, since the default toString method of Double does exactly what you need.... ...the previous poster is right- not too belabor the point, but this question isn't very advanced.
-
Vector to String[] Conversion
Hi All,
I am trying to convert Vector to String Array. the problem is when I tried to print the String Array out side the loop I am getting only last value.
I want to pass String Array to another function.
How to do that?
Find the code below:-
Vector vec=new Vector()
vec.add("abc\\aa.doc");
vec.add("abc\\ccc.doc");
vec.add("abc\\bb.doc");
String [] arr=new String[vec.size()];
for(int i=0;i<=vec.size();i++){
arr=vec.get(i).toString();
System.out.println( "ARRAY" + arr);
Current Output
Output :- ARRAY abc\\bb.doc
Desired Output
ARRAY abc\\aa.doc,,abc\ccc.doc,abc\\bb.doc
Thanks.OK,
My vector contains Objects...
I tried to use the following code
String[] attachments = new String[setOfHandle.size()];
for (int i = 0; i < setOfHandle.size(); i++) {
attachments[i] = String.valueOf(setOfHandle.get(i));
// if attachments exists, add them
if ( attachments != null ) {
addAtta( maild, attachments );
checkPoint = false;
}In the above code setOfHandle is a vector contains objects.. I need to convert it to String[] and pass it to addAtta.
While doing this I am getting an error
java.lang.ArrayIndexOutOfBoundsException: 1 -
Converting a vector to an array
hello :-)
i am converting my vector into an array with this syntax:
String[] lmname = (String[])name.toArray();
and try to print my array with
for(int i=0; i<lmname.size; i++)
System.out.println(lmname);
unfortunately, i get this error:
java.lang.ClassCastException
what did i do wrong?-- you cannot do that...
- if you still want to convert Vector into String[] then you can do this:
Vector v=new Vector();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
String strVector[]=new String[v.size()];
for (int i = 0; i<v.size(); i++)
strVector=new String((String)v.get(i));
for (int i = 0; i<strVector.length; i++)
System.out.println (strVector[i]);
... By the way, you can use List instead of Vector,,, -
How to conver vector to Int array
Hi
I know toArray method can be used to convert vector to an array but what if I want to convert it to an int array?
Vector v = new Vector();
int r [] = new int [1];
v.add(2);
r = v.toArray()//gives errorHow can I cast it to return int Array rather than object array?Vector v = new Vector(10);
for(int i = 0; i < 10; i++) {
v.add(i);
int r[] = new int[v.size()];
for(int i = 0; i < r.length; i++) {
String value = v.elementAt(i).toString();
r[i] = Integer.valueOf( value ).intValue();
System.out.println(r); -
DateTime String array in Graph
Greetings.
I tried to plot DateTime string array VS Numbers array with no success in the XY Graph object.
I even converted the DateTime string array to DateTime type using the VI called "Convert_String_to_TimeStamp.vi" with also no success as the X (Time) is just very big numbers and a datetime format.
Please check the attached shot, it will describe the situation clearly.
Thanks in advance
Ayman
Ayman Mohammad Metwally
Automation Engineer
Egypt - Cairo
Solved!
Go to Solution.
Attachments:
Graph.JPG 93 KBYou could read the file line by line and use the scan from sting function to convert to a timestamp.
Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
If you don't hate time zones, you're not a real programmer.
"You are what you don't automate"
Inplaceness is synonymous with insidiousness -
How to convert String array into int.
void getSoldSms(Vector vecSoldSms)
String str[]=new String[vecSoldSms.size()];
String words[]=new String[str.length]; // String array
for(int i=0;i< vecSoldSms.size();i++)
str=(String)vecSoldSms.get(i);
} //End for
for(int i=0;i<str.length;i++)
words = str[i].split("\\|\\|");
System.out.println();
for(int j=0;j<1;j++)
int count[str.length]=Integer.parseInt(words[i]);
System.out.print(count[j]*advance_count);
} // end inner for loop
} //End for
} //End function getSoldSms
how do i convert words which is a string array into int type. i kno string can be converted into int using interger.parseint. but wat abt string arrays??? plz help me out with the above code.i did tht its still giving the same errorFor Heaven's sake, what about taking a second to try to understand the code you're copying first? If you really can't fix the error yourself, you have a more serious problem than just convertingStrings to ints.
And if you want { "1", "2", "3" } to be 123:
StringBuffer b = new StringBuffer();
for (int i = 0; i < array.length; i++) {
b.append(array);
int result = Integer.parseIn(b.toString()); -
Is there an easy way to convert a long string into an array?
I can convert a long string into a 1-d array by parsing and using build array, but I would like to know if there is a function to make this easier.
For example:
from/ aaaaaaaabbbbbbbbccccccccdddddddd (string of ascii)
to/ an array that is 1-d with each element having eight characters
aaaaaaaa
bbbbbbbb
cccccccc
dddddddd
Thank you.
Solved!
Go to Solution.Try something like this:
(If you can guarantee that the string length is an integer multiple of 8, you an drop the two triangular modes in the upper left. )
Message Edited by altenbach on 03-14-2010 06:40 PM
LabVIEW Champion . Do more with less code and in less time .
Attachments:
ChopString.png 9 KB -
Converting from spreadshet string to array and then back to spreadsheet string
My questions is; why is the Spreadsheet string to array function creating more data than the original string had when you change the array back into a spreadsheet string. Im trying to analyze a comma delimited file using array functions since my column and row size is constant, but my data varies. Thus my reason for not using string parsing functions which would get more involved and difficult. So, however, after i convert to a 2D array of data from the comma delimited file I read from, and then I convert back to string using the Array to Spreadsheet String, I get added columns to the file, which prevents another program from receiving these files. Also, the data which I am reading is not all contiguous, it has gaps in some places for empty data. Looking at the file compared to the original after it has gone from string to array and then back to string again, looks almost identical except for the file size which got larger by 400 bytes and where the original file has empty spaces, the new file has a lot of commas added. Any idea?
CharlesThe result you get is normal when the spreadsheet string contains rows of uneven length. Since the array rows have the same number of elements, nil values are added during the coonversion. And of course, the back to string conversion keep those added values in the string, with the associated commas.
example : 3 x 3 array
1,2,3
4
5,6,7
is converted into
1 2 3
4 0 0
5 6 7
then back to
1,2,3
4,0,0
5,6,7
Chilly Charly (aka CC)
E-List Master - Kudos glutton - Press the yellow button on the left... -
Text file convert to string array
I tried to convert text file to string array. But it is not successful.
text file :
SD, 1,2,3,4
GD, 3,4,5,6
I use spreadsheet string to Array function but ALL characters is become to zero.
my result:
0,1,2,3,4
0,3,4,5,6
Solved!
Go to Solution.
Attachments:
convert.vi 18 KBHi,
indeed the solution of GerdW is the way to do it, except for the fact that the while loop isn't needed in this case
Have fun using LabVIEW
Kind regards,
- Bjorn -
Have fun using LabVIEW... and if you like my answer, please pay me back in Kudo's
LabVIEW 5.1 - LabVIEW 2012
Attachments:
Speadsheet_to_array.JPG 27 KB -
Why does the last line cause a ClassCastException?:
Vector vector = new Vector();
vector.add("One");
vector.add("Two");
vector.add("Three");
vector.add("Four");
vector.add("Five");
String[] array = (String[])vector.toArray();
Thanks
[email protected]Because toArray () creates an Object array. You need the toArray(Object[]):String[] array = (String[]) vector.toArray (new String[vector.size ()]);Kind regards,
Levi -
How to convert an int array to string array?
Hi,
Can anyone please help me with this question?
int number={4,6,45,3,2,77};
I like to convert this into a list and sort
List mylist = Arrays.asList(number);
Collections.sort(mylist);
But first I need to convert the int array to String array.
Please advise. Thanks.If you want to convert your int array to a String array, you have no choice but doing a conversion element by element in a for loop.
However, if the sort method doesn't behave as desired, you can use the one with 2 parameters, the second parameter being a comparator class.
Check the javadoc for more information : http://java.sun.com/j2se/1.3/docs/api/java/util/Collections.html
/Stephane -
Converting String Array -- String
Hi All,
I am converting String array to string using the following code:
String[] a= ....;
StringBuffer result = new StringBuffer();
if (a.length > 0) {
result.append(a[0]);
for (int i=1; i<a.length; i++) {
result.append(a);
return result.toString();
Is there is any other easy or efficient way to convert rather than the above code ?
Thanks,
J.KathirIt could have been written:
StringBuffer result = new StringBuffer();
for(int i=0; i<a.length; ++ i)
result.append(a);
return result.toString();
Or in 1.5 lingo
StringBuilder result = new StringBuilder(); //slightly less overhead
for(String s : a)
result.append(s);
return result.toString();If you aren't picky about the format of the resulting string,
you could use the java.utilArrays method )]toString(Object[]):
String[] array= {"Hello", "World", "this", "is", "a", "1.5", "method"};
String s = Arrays.toString(array); //[Hello, World, this, is, a, 1.5, method] -
SIMPLE QUESTION! converting an Enumeration to a String array;
Enumeration e=request.getParameterNames();
i have an enumeration that contains some String objects
how can i convert it to a String array?
thanks a lot.String str = null;
List list = new ArrayList();
while(e.hasMoreElements()) {
str = (String)e.nextElement();
list.add(str);
String[] strArray = (String[]) list.toArray();
Disclaimer: Code not tested; use at your own
risk ;)Taht piece of code will probably throw class cast exception at the line:
String[] strArray = (String[]) list.toArray();The array returned is an object array. You should use:
String[] strArray = (String[]) list.toArray(new String[list.size()]);That methods returns an array of the same type as the argument.
/Kaj
Maybe you are looking for
-
Valid Apple ID but not an iCloud account?
I have valid Apple ID/password. Trying to setup iCloud and after downloading necesary files, etc. and attempting to setup I receive iCloud sign in window, but when I attempt to sign in it states "This is valid Apple ID but is not iCloud account." How
-
hi all, i am creatinf a smartform for PO. in this i have to specifically print item changed in front of item in case it is changed. now my query is is there any fm or any other way through will i can find the changes along with the old values . i don
-
Hi Sdn friends, I Created the New ISR Scenario which is designed in Adobe Form, i tested form through the R/3 by clicking the Test Button, it is working. but i am not able to see that form in MSS Portal (mystaff>PCR>New Pcr) in show data dropdown i c
-
How do I find out what my security question are ?
I've tried to down load an I tune and they are asking me to answer two questions fav job fav football team I answerd the questions then it came back as wrong answers so can you pleas tell me how I can sort this ??
-
Why do I get an "Array element prototype" -17001 error when trying to "View Paths"?
I added some custom types to my sequence file and to MyTypes.ini, and then opened and updated all of the .seq files under "Components/User". I'm a TestStand Newbie, but I think I did this correctly. Now, if I try to "View Paths" in the sequence file,