Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I cast from Vector to String Array?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to cast from an existing vector of string values to a new String Array. ie.
String myArray[] = (String[]) myVector.toArray();
When I do this, it crashes...
please help.. already wrote around it a function to return a string array after passing the vector into it, but thats stupid, and its late and any help on the correct syntax would be greatly appreciated.
 
Author
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object[] yourArray = yourVector.toArray()
etc. Vector is not of type array so you cant cast to an array. You can retrieve the array as objects and then cast each object to string as you are retrieving it
for(blah blah) {
String thisString = (String)yourArray[i];
etc
 
Leon Gelbak
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried that syntax. Below is the result
org.apache.jasper.JasperException: Unable to compile class for JSPD:\inetpub\wwwroot\dev\localhost_8080\_0002fadmin_0002fuser_0002fUserListUpload_0002ejspUserListUpload_jsp_12.java:319: Incompatible type for declaration. Explicit cast needed to convert java.lang.Object[] to java.lang.String[].
String[] amyRoles = myRoles.toArray();
^
1 error
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's why the original code said Object[] The method as described returns an Object array, the elements of which just happen to be Strings, not a String array.
There are two forms of toArray.
One, as described above, creates an Object[] of the correct size and copies all the elements of the collection into it.
Another, not mentioned yet, takes a "prototype" object as a parameter, and creates a return array of the same type.
So if you really need a String[] (and can't take the hit of just casting the Objects from the array to String when they are extracted), then do something like:
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Frank Carver:

There are two forms of toArray.
[/CODE]


Sweet. Never noticed that before. I've always used System.arraycopy:
[code]
Object arr1[] = {"asdf", "jkl;", "qwer", "uiop"};
String arr2[];
System.arraycopy(arr1, 0, arr2, 0, arr1.length);
[/code]
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic