•
•
•
•
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
![]() |
•
•
Join Date: Nov 2004
Location: UK
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,661
Reputation:
Rep Power: 18
Solved Threads: 192
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:
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);
•
•
Join Date: Apr 2007
Posts: 14
Reputation:
Rep Power: 2
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<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();
}
}
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();
}
}
•
•
•
•
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
----------------------------------------------
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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
- String to array (C++)
- how to cast a string type variable to LPCTSTR type (C++)
- Declaring string array, initializing later... (C++)
- Copy string into an array (C#)
Other Threads in the Java Forum
- Previous Thread: Seeking help in Java Projects
- Next Thread: Status bar in Main Menu



Linear Mode