• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Casting of Object[] to Animal[] fails

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Casting of Object[] to Animal[] fails while the Object[] array contains only Animal objects
when i use a for loop to add each Animal out of the Object[] array to the Animal[] array it works.. but like stated just trying to cast it in one line of code (Animal[]) fails
why is that?
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the code you're attempting? It seems to work fine for me.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you creating the Object[]? Because only putting Animals into an Object[] is not enough to make it an Animal[]. You need to have actually created an Animal[] in the first place.
 
Alexander Curvers
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you both for your replies

Matthew Brown wrote:How are you creating the Object[]? Because only putting Animals into an Object[] is not enough to make it an Animal[]. You need to have actually created an Animal[] in the first place.



with [List].toArray() it makes a Object[] so its clear that is the problem maybe there is a better way to convert the list to an array? so it can be cast or doesnt need to be casted

 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, thought it might be that! There's a trick. You need the version of toArray that takes an argument.

<T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.


So if you try:
it'll return an Animal[].
 
Alexander Curvers
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah thanks Matthew! thats what i needed
 
Marshal
Posts: 22401
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Slightly better, because it creates only one array (with elements) instead of two (one empty one, one with elements):
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic