aspose file tools*
The moose likes Beginning Java and the fly likes Trouble with generics - using a class method within a generic function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with generics - using a class method within a generic function" Watch "Trouble with generics - using a class method within a generic function" New topic
Author

Trouble with generics - using a class method within a generic function

Stuart Rogers
Ranch Hand

Joined: Oct 02, 2008
Posts: 133
Greetings all,

I've several classes, each consisting of an assortment of Strings and each containing a function getAsArray which returns the contents of the object as a String array.
Here an example from one specific class named Asset_HP :


In the program I populate several LinkedLists , one for each class.

I'd like to craft one generic function to write out a LinkedList like so:


instead of hardcoding a Write function for each LinkedList like this specific example for one list and one class:


what do I need to make this work? Or should I be using some other approach?

TIA,

Still-learning Stuart
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

I believe you'll need to either define getAsArray() in an abstract class or an interface and then do something like:



GenRocket - A Test Data Generation Platform
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Stuart Rogers wrote:what do I need to make this work?

Something other than Java, due to this line:As Gregg says, you'll need to work around the fact that Java's generics are available at compile-time only.

The variable names "lisst" and "roww" confuse me; do the extra letters have some internal significance?
Stuart Rogers
Ranch Hand

Joined: Oct 02, 2008
Posts: 133
I deliberatley misspell my variable names so they can never be confused with typenames or anything that could be construed as part of the language itself. How many times have you seen this in a textbook?

List list = new List();

or this from C++ :

typedef int LENGTH;
LENGTH length;

in Java lots of objects inherit a .length() method.

Back to the question at hand, perhaps I should suppress warnings in my IDE for that particular function and try it "as-is".


Thanks for your replies so far,
Still-learning Stuart
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I don't see what the problem is with the examples you provide, but whatever.

Ideally variables should be named what they are; "list" is a sub-optimal name.
Stuart Rogers
Ranch Hand

Joined: Oct 02, 2008
Posts: 133
SUCCESS!!

I solved the problem by using an interface :


making sure all my object classes implement Asset

and in the main codeblock changing all references to the many object classes to <Asset> and Asset, respectively.

Thanks to all for pointing me in the right direction!

CASE CLOSED
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble with generics - using a class method within a generic function
 
Similar Threads
a Factory for creating LinkedLists of Objects
how to serialize String[ ][ ]
Are only assignment statements allowed in a class' constructor?
PrintWriter won't write to a text file.....
more trouble with Generics - inserting in to a base class