Jump to content

Java question [answered kind-of]


Recommended Posts

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 by neddie
Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...