User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 397,771 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,511 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 22925 | Replies: 4 | Solved
Reply
Join Date: Nov 2004
Location: UK
Posts: 6
Reputation: grussell is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
grussell grussell is offline Offline
Newbie Poster

Help How do I convert a vector to a String array ?

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,661
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 192
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

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

  #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:
String[] criteria = new String[tokenVector.size()];
tokenVector.toArray(criteria);
Reply With Quote  
Join Date: Jul 2007
Posts: 1
Reputation: srijyothsna is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
srijyothsna srijyothsna is offline Offline
Newbie Poster

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

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

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

  #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  
Join Date: Feb 2006
Posts: 1,363
Reputation: masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough 
Rep Power: 8
Solved Threads: 117
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

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

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 4:26 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC