944,198 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 69661
  • Java RSS
Dec 20th, 2004
0

How do I convert a vector to a String array ?

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grussell is offline Offline
6 posts
since Nov 2004
Dec 20th, 2004
0

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

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:
Java Syntax (Toggle Plain Text)
  1. String[] criteria = new String[tokenVector.size()];
  2. tokenVector.toArray(criteria);
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jul 2nd, 2007
0

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

Thanks a lot!!
The solution has helped me with ArrayList<String> to String[] conversion also in java.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
srijyothsna is offline Offline
1 posts
since Jul 2007
Jul 4th, 2007
0

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

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();
}
}
Reputation Points: 30
Solved Threads: 4
Newbie Poster
deng_cen is offline Offline
14 posts
since Apr 2007
Jul 4th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by deng_cen ...
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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Seeking help in Java Projects
Next Thread in Java Forum Timeline: Status bar in Main Menu





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC