• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Mock question of collection's toArray method

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Keith . I know what you mean.

I change


to



Then it works.
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic