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.
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
chanoch<p><a href="http://www.amazon.com/exec/obidos/ASIN/1861007736/" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
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
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: