File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toArray() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "toArray()" Watch "toArray()" New topic
Author

toArray()

David A. Scott
Ranch Hand

Joined: Apr 13, 2006
Posts: 55
Hi, can you help me clarify a couple of things on the toArray() method of List objects?

Firstly, when I compile the following code



I get :-



My understanding was that this version of the overloaded toArray method accepted an object of the type you wanted it to return. What am I doing wrong in this instance?

Cheers!

David

[ October 19, 2006: Message edited by: David A. Scott ]
[ October 19, 2006: Message edited by: David A. Scott ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You need to cast the value returned from the method to the appropriate type of array.
David A. Scott
Ranch Hand

Joined: Apr 13, 2006
Posts: 55
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?

i.e.



And doesn't the following example from K&B suggest that no casting is required?

wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
How to use the toArray() method of Collection?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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?

i.e.



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.
Venkat Sidh
Ranch Hand

Joined: Sep 30, 2006
Posts: 61
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
Ranch Hand

Joined: Apr 13, 2006
Posts: 55
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[0]) 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.

Thanks.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toArray()