Casting Arrays
Could someone explain why you cannot cast an array of Objects into an array of Strings?
Specifically, I have been trying to use this bit of code:
String stringArray[];
ArrayList arrayList = getArrayList();
stringArray = (String[])arrayList.toArray();
I found that you must use the toArray(stringArray) method, but I don't understand why you can't use the code above.
Thanks!
[426 byte] By [
Rewes] at [2007-9-26 1:19:20]

When you cast, you cast a reference, not an object.
The class of object the reference points to is checked to see if it is a subtype of the type the reference is being cast to. If it is not, a ClassCastException is thrown.
In your case, the object is an instance of class Object[]. You are trying to cast a reference to this object to the String[] type. This will not work, because Object[] is not a subtype of String[].
To put it another way: you cannot cast a reference to an Object instance into a String reference. For the same reason, you cannot cast a reference to an Object[] instance to a String[] reference.
Try this code:
stringArray = (String[]) arrayList.toArray(new String[0]);
This causes the toArray method to return an Object[] reference to a String[] object. You can cast this reference to a String[] reference.
Sorry, still a little confused here. You say that you can't cast a reference to an Object instance into a String reference. What is occuring when you use this statement?
ArrayList list = getArrayListOfStrings();
String s = (String)list.get(0);
Isn't the get method returning a reference to an Object instance?
Rewes at 2007-6-29 0:51:41 >

I was having the same problem recently. All you need to do is put parenthesis around the object and method. So your statement would appear as follows:
String stringArray[];
ArrayList arrayList = getArrayList();
stringArray = (String[])(arrayList.toArray()); // <-- change
That worked for me. In your original example, the VM thinks you're trying to cast the ArrayList to an array of Strings, and so will throw an exception. This way, it knows you're referring to what the toArray() method is returning.
> I was having the same problem recently. All you need
> to do is put parenthesis around the object and method.
> So your statement would appear as follows:
>
> String stringArray[];
> ArrayList arrayList = getArrayList();
> stringArray = (String[])(arrayList.toArray()); // <--
> change
>
> That worked for me. In your original example, the VM
> thinks you're trying to cast the ArrayList to an array
> of Strings, and so will throw an exception. This way,
> it knows you're referring to what the toArray() method
> is returning.
Sorry... this is totally wrong. Casting has a lower precedence than a method call, so the parens are not needed.
You cannot cast a reference to an Object[] to a String[], for exactly the same reason you cannot cast a reference to a Object to a String -- an Object is not a String.
You can pass toArray a String[] object, and it will return an Object[] reference to a String[] object. You can cast this reference to a String[] reference:
String[] stringArray = (String[]) arrayList.toArray(new String[0]);
> Sorry, still a little confused here. You say that you
> can't cast a reference to an Object instance into a
> String reference. What is occuring when you use this
> statement?
>
> ArrayList list = getArrayListOfStrings();
> String s = (String)list.get(0);
>
> Isn't the get method returning a reference to an
> Object instance?
Here's my understanding...
Assuming getArrayListOfString() is a method that returns an ArrayList whose contents are Strings, then the get() method returns an Object that is a String, so the cast works. If you add(aString) to a list, then get() casts the String to an Object and returns the Object reference. On the other hand, the toArray() method probably contains a statement like "Object[] return = ..." so what it returns is an Object array, not a String array, so the cast fails. The other form of toArray() creates an array of the right type, so it can be successfully cast.
>On the other hand, the
> toArray() method probably contains a statement like
> "Object[] return = ..." so what it returns is an
> Object array, not a String array, so the cast fails.
> The other form of toArray() creates an array of the
> right type, so it can be successfully cast.
Not quite. The code you show above causes the type of reference to be Object[], but that will not prevent the reference from being cast to a String[].
Compare these two ways of creating an Object[] reference:
Object[] oa = new Object[10];
Object[] sa = new String[10];
oa is a reference to an Object[] object, and cannot be cast to a String[]. sa is a reference to a String[] object, and can be cast to a String[].
You're right, I should have included = new Object[size()] or something like that. But I think I have the concept - the new ... creates an instance of an object and you can't cast an instance of Object[] to an instance of String[].