aspose file tools*
The moose likes Java in General and the fly likes Generic arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generic arrays" Watch "Generic arrays" New topic
Author

Generic arrays

Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Howdy fellow coders,

I have a question about generic arrays. I have this function.

Which compiles when placed in a my ArrayUtil class. However whenever I use it I get an exception.
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.StackTraceElement;

This is probably because of my (T[]) cast. The compiler won't let me create a generic array T. So how can I fix this problem?

Thanks in advance.


Olly
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 511
Hello,
Check this out.
Check method -
I think you also need to pass another param to mention what type of array you are handling.

Regards,
amit
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Hi Olivier,

Please, TellTheDetails (i.e. exactly when you are having this exception).

It took me a little type to figure this out that the exception occurs when you actually try to assign output of subset method to an array

As amit has suggested, either you should provide class type of T to the method (which will return the array of type T, instead of array of Object), or, you have to get the returned value in an Object array, and later-on you can cast it to real type T.

HIH.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic arrays