• 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

java Generics

 
Greenhorn
Posts: 8
Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In some part of my code I need to invoke a third party function which has a signature like this

public static <T> T[] functionToProcess(T[][] a_rrs, Class<T[]> a_destType)

From the signature I make out that the function accepts a generic 2-dimension array and a Class object referring to single dimension array of the same generic type as parameters.

I tried to invoke the function like this





and received an error.

Any idea where is has gone wrong?

Thanks,

Mamatha
 
Ranch Hand
Posts: 32
Eclipse IDE Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

Take a look at getClass() method documentation. Why don't you use File[].class instead?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you post the exception
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you're calling theoutput.getClass() before you've defined theoutput, so that's never going to work. Marcin's suggestion should fix that - you might as well use a class literal if you can.

One further point - casting the return value to File[] is unnecessary - the return type is T[], so it will already be File[] in that call.
 
Mamatha Achuthan
Greenhorn
Posts: 8
Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The File[].class works! Thanks.


The Netbeans IDE was showing the following error message when I used the getClass function

Method cannot be applied to given types
found : java.io.File[][],java.lang.Class<capture #1 of ? extends java.io.File[]>
required : T[][],java.lang.Class(T[])

Thanks again,

Mamatha
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because getClass() returns a Class<?>, without any generic information. The main way to get a Class object with generic information is to use a class literal. Class.getSuperclass and Class.asSubclass return a Class object with some generic information but it still has a wildcard type.
 
Mamatha Achuthan
Greenhorn
Posts: 8
Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for the information you all have shared. Thanks to you all, I took a look the 'Class' class and leaned new things.



Thanks again,

Mamatha
reply
    Bookmark Topic Watch Topic
  • New Topic