wood burning stoves 2.0*
The moose likes Java in General and the fly likes vector to String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "vector to String" Watch "vector to String" New topic

vector to String

Maria Krishna

Joined: Nov 07, 2003
Posts: 3
Hi there,
when i converted a vector to a string array using toArray, i had a classCastException, How do I convert a vector to String object
thanks in advance
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
As long as you are assured that every element in the vector is a String, try this:

The "toArray()" method in Vector is overloaded; if you don't call the correct one it will only return an array of Object. If you want to cast it to some other type, you have to pass in a "seed" array of that type. As shown in this example, it can have a length of zero.
[ November 07, 2003: Message edited by: Wayne L Johnson ]
Joe Ess

Joined: Oct 29, 2001
Posts: 8843

However, if you are not assured that every object is a String, you will have to iterate through the Vector and invoke toString() on each of the Vector's elements (adapting the previous example):

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: vector to String
Similar Threads
A question about generic type
EduardoCobian Q 29
Using result sets, statements & connections
decimal place formatting JtextField
declaring a new Vector