Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Mock question of collection's toArray method  RSS feed

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above code have runtime exception ClassCastException on line 1 since s.toArray return object[], and it cannot cast to String[].

Do you guys have any way to change it to String[].

Thanks in advance.
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes there is another method called toArray that accepts a reference to an array.
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that API. s.toArray(T[] a). But I do not have an array before calling toArray method.

So I cannot do it in that way.
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can create one.

You know the type of the array, and you know how many elements will be in the array.
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then why I need toArray method? For fun.

Just kidding. I mean I need use toArray method to get an array. If I already have an array, why I need use toArray method.

You know what I mean.
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is simple.

If you have a Set containing objects that are of type String, if you only use the toArray() method, then you get an array of type Object[].

You cannot cast this to String[].

If you wanted to have a String array, you would have to create a new String array and copy the elements from the Object array, casting each one to type String if the type of the Object were String.

With the version of toArray that accepts an array, the implementation can create an array of the specified type and populate it with the elements in the Collection.

Before generics, you would have to cast the result of the method to that particular type of array reference.

With generics, you are returned a reference to an array of the type in the Collection.
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Keith . I know what you mean.

I change


to



Then it works.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!