• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Cannot return a generic array

 
Greenhorn
Posts: 20
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created my own generic class as follows:



I have utilized the MyGeneric class in MyGenericUtilizer class as follows:



The functions insertValue and returnValueAt work properly. But the function returnArray() throws the following exception:


What is causing this exception?

I also tried initializing the generic list as follows:


This compiled fine. But on execution the insertValue() function threw java.lang.ArrayStoreException
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is supposed to be impossible to create an array of type T<U>[]. There should be more details in the Java Tutorials: try here and here. It has to do with type erasure, that it might be possible to add the wrong type if that were permitted.
 
Campbell Ritchie
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would appear to be returning an Object[], because generic types are erased at run‑time. You cannot assign an Object[] to a String[] reference. You can try casting it to (String[]), but by using an explicit cast, you are abandoning the type‑safety provided by generics.
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line here:

You're creating an Object[]. You can't make it any other type of array just by casting it - it's still an Object[]. So when you then try and assign it to a String[] it's not going to work regardless of what T is.

Arrays and generic aren't straightforward - see http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html for some more information about this (see the section "Concrete Instantiations"). I generally try and avoid using them together to avoid having to worry about the details - life is easier if you stick to generic collections.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Bijapurkar wrote:I also tried initializing the generic list as follows:

This compiled fine. But on execution the insertValue() function threw java.lang.ArrayStoreException


Because, as with your cast Object[], the array is the wrong type. What you need is a T[], and you can only do that by supplying T.class (or indeed a T[] array, as with Collection.toArray(T[])).

However, you're much more on the right lines using Array.newInstance(), because that will return an array that IS useable as a T[]. You just need to get the class right.
One possiblilty to consider might be a static factory method, viz:
public static final <T> MyGeneric<T> newInstance(Class<T> elementClass, int length);

but, as Matthew said, you're probably far better off just using generic collections or Lists internally.

Winston
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic