• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

generic and arrays

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello!

could someone suggest what a solution for the following code would be?



at the moment i have to up cast everything to Object as the above is illegal:


this creates a problem as if i wanted to iterate through the array using the new syntax:



becomes illegal.

thanks,
tian
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See if this helps.
 
Tian Zhang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,

thanks for the link, though unfortunatly i didnt find it that useful i'm sure it will be useful to others!

i've ended up with some ugly casting as googling: generic arrays by luck landed me on page that comfirmed what i had suspected in the first post.

thanks,
tian
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One option is to not use arrays at all, and use List<MyType> instead. Another, if you really really need arrays of the appropriate type, is to have your instance keep a reference to the Class it's associated with (require it in the constructor or factory method). Then use java.lang.reflect.Array.newInstance(clss, size) to create the appropriate array.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tian Zhang:
hello!

could someone suggest what a solution for the following code would be?



at the moment i have to up cast everything to Object as the above is illegal:


this creates a problem as if i wanted to iterate through the array using the new syntax:



becomes illegal.

thanks,
tian



You have found (one of many limitations) a limitation of generics. You'll find evidence of your best case scenario in places such as the source to java.util.ArrayList or net.tmorris.adt.set.MutableSet, which amounts to a compile-time warning (or alternatively, something other than an array without O(1) seek).

Welcome to Java
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic