• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

toArray()

 
David A. Scott
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to cast the value returned from the method to the appropriate type of array.
 
David A. Scott
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic