neddie Posted January 11, 2007 Report Share Posted January 11, 2007 (edited) Well we've had some java questions on here before, so here goes... Can anyone explain to me why this code doesn't work? I'm trying to convert an array of Objects into an array of Strings: Object[] colours = {"red", "green", "blue"}; String[] colourStrings = (String[]) colours; // FAILS but it just throws a ClassCastException at runtime, rather to my surprise. I can cast an Object into a String (if it is one), so why can't I cast an array of Objects into an array of Strings? Seems weird. Is the only way to convert it to make a new array of the same size, loop over all the elements and cast each one in turn? Sounds hairy if the array is big. Edited January 11, 2007 by neddie Quote Link to comment Share on other sites More sharing options...
tyme Posted January 11, 2007 Report Share Posted January 11, 2007 perhaps try some of the suggestions here. Quote Link to comment Share on other sites More sharing options...
phunni Posted January 11, 2007 Report Share Posted January 11, 2007 I think it's something to do with the fact that an Array is itself an Object (and therefore a type) so it's not quite as simple as with other objects... You'd be better off creating a Vector full of the Strings and then converting that to an array of Strings: String[] colouredStrings = (String[]) myColoursVector.toArray(new String[0]; Actually, you'd be better off just starting with an array of Strings - but I'm assuming there's a reason you're not... Quote Link to comment Share on other sites More sharing options...
javaguy Posted January 11, 2007 Report Share Posted January 11, 2007 You can reference individual elements of the array and cast those to type String, but you cannot cast the array object itself to an array of Strings because, well, that's not what it is. It's an array of Objects. Quote Link to comment Share on other sites More sharing options...
neddie Posted January 11, 2007 Author Report Share Posted January 11, 2007 Thanks guys. Yes tyme that link was helpful, the question was exactly what I had in mind but unfortunately the answers were all "no, you have to loop". I understand that Vector.toArray method, the thing that was confusing me was that in that example the toArray method actually returns an Object[] array, and in that particular case it's possible to cast the return value directly into a String[] array. So I thought you should be able to cast my example colours array in the same way. But it's not possible. Similarly, if you call the other Vector.toArray() method (the one without any parameters) you also get an Object[] array back, containing the same String objects, but in that case you can't cast the result into String[]. :unsure: phunni: it's a strange one because it's often possible to cast arrays - for example I can cast my String[] into an Object[] without problems. And in your own example the cast from Object[] to String[] also works. That's why I was confused, I figured it would just work. Because, as you say, arrays are objects and objects can be casted to other classes. And your example, although it should work, looks horribly expensive. javaguy: True but you could argue the same for this: Object colour = "red"; String colourString = (String) colour; Again it's not a String, it's an Object, yet you can cast it into a String. So I thought the same would apply to arrays. Surprised me more than a little to find out that it doesn't. Quote Link to comment Share on other sites More sharing options...
FX Posted January 11, 2007 Report Share Posted January 11, 2007 Here it was because I thought you had color spelled/typed wrong. :P Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.