954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do I convert a vector to a String array ?

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.

grussell
Newbie Poster
6 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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);
jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Thanks a lot!!
The solution has helped me with ArrayList to String[] conversion also in java.

srijyothsna
Newbie Poster
1 post since Jul 2007
Reputation Points: 10
Solved Threads: 1
 

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 list = new ArrayList();
// 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();
}
}

deng_cen
Newbie Poster
14 posts since Apr 2007
Reputation Points: 30
Solved Threads: 4
 
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 list = new ArrayList(); // 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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You