How do I convert a vector to a String array ?

Thread Solved

Join Date: Nov 2004
Posts: 6
Reputation: grussell is an unknown quantity at this point 
Solved Threads: 0
grussell grussell is offline Offline
Newbie Poster

How do I convert a vector to a String array ?

 
0
  #1
Dec 20th, 2004
I want to convert a vector into a string array. I have put in my code:

criteria = (String []) tokenVector.toArray();

I get a ClassCastExceptionError (I can understand why since Vector holds objects which are 'bigger' than strings) - and yet I cannot understand how to resolve it - any ideas would be welcome.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: How do I convert a vector to a String array ?

 
0
  #2
Dec 20th, 2004
you need to use an overloaded version of toArray() which takes as argument an array to put the data in (if it fits).
The usual way to use this version is as follows:

criteria = (String[])tokenVector.toArray(new String[0]);

which gives the function a zero-length array of String with the only purpose to use this array as an indication of the type of array you want it to return.
Alternatively you could initialise the array with the correct length and pass it:
  1. String[] criteria = new String[tokenVector.size()];
  2. tokenVector.toArray(criteria);
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1
Reputation: srijyothsna is an unknown quantity at this point 
Solved Threads: 1
srijyothsna srijyothsna is offline Offline
Newbie Poster

Re: How do I convert a vector to a String array ?

 
0
  #3
Jul 2nd, 2007
Thanks a lot!!
The solution has helped me with ArrayList<String> to String[] conversion also in java.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 14
Reputation: deng_cen is an unknown quantity at this point 
Solved Threads: 4
deng_cen deng_cen is offline Offline
Newbie Poster

Re: How do I convert a vector to a String array ?

 
0
  #4
Jul 4th, 2007
hi:
you can do that like that is
package com.structure;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TestArray {
static List<String> list = new ArrayList<String>();
// static Object[] state;
public static void main(String agrs[]) {
Scanner scan = new Scanner(System.in);
System.out.println("Please input number:");
int num = scan.nextInt();
System.out.println("Please input your String:");
for (int i = 0; i < num; i++) {
list.add((String) scan.next());
}
for (Object str : exchangeArray(list)) {
System.out.println(str);
}
}
public static Object[] exchangeArray(List list) {
return list.toArray();
}
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,357
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: How do I convert a vector to a String array ?

 
0
  #5
Jul 4th, 2007
Originally Posted by deng_cen View Post
hi:
you can do that like that is
package com.structure;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TestArray {
static List<String> list = new ArrayList<String>();
// static Object[] state;
public static void main(String agrs[]) {
Scanner scan = new Scanner(System.in);
System.out.println("Please input number:");
int num = scan.nextInt();
System.out.println("Please input your String:");
for (int i = 0; i < num; i++) {
list.add((String) scan.next());
}
for (Object str : exchangeArray(list)) {
System.out.println(str);
}
}
public static Object[] exchangeArray(List list) {
return list.toArray();
}
}
He could do it something like, but then the question is, Why would he, when he as already been shown two better, and easier to use, solutions.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC