Originally posted by David A. Scott: I thought using this version of the method meant that it returned the appropriate type of array.
What is the difference then between toArray with a parameter and without a parameter?
And doesn't the following example from K&B suggest that no casting is required?
In your example, you are using a raw list.
In the example from K&B, they are specifying the type that is held in the Collection.
Joined: Sep 30, 2006
This question is more of using Generics and not.
From K & B,
There's nothing too fancy going on with the toArray() method; it comes in two flavors: one that returns a new Object array, and one that uses the array you send it as the destination array:
David A. Scott
Joined: Apr 13, 2006
From the link supplied by Owen Wise :-
"toArray() without parameter will always return an array of type Object that you won't be able to cast. The toArray(new String) will return an array of type Object that you may cast to String."
I think that's my question answered in the first paragraph. The cast is still required.
Joined: Feb 07, 2005
It depends on whether you use a parametrized collection or not.
If you specify the type to be held in the Collection, then, using the toArray method with the appropriate parameter, you can cast to the original type of array, but it's not necessary.