favoritePet = mylist.get(0); // errors because favoritePet doesn't know it came from a Cat
But what about when I don't use a collection object "in the raw"?
LinkedList<Cat> mylist = new LinkedList<Cat>();
Cat blackCat = new Cat("black");
favoritePet = mylist.get(0); // I'd hoped to make this work somehow
Or is it possible to extract the type of the stored objects ( Cat ) from a Collection with a specified type?
Joined: Oct 13, 2005
Collections don't store the actual parameter; so the <Cat> bit is lost. This is because Java implements generics by erasure.
What I meant about object-oriented is that you should have a field in your resource which stores the manufacturer's name. You can put those names into an enum.
Joined: Oct 02, 2008
Back again armed with more code.
Essentially, I have several kinds of similar assets and I'd like to place each kind of asset in its own list.
Creating a class for each specific type of asset and having them all share an interface to expose a common method seems logical.
Been busy reading and it sounds like Generics, with a little help from reflection, will do what I need.
So I created a generic AssetLinkedList class that contains a .populate() and writeOut() method.
The idea is to instantiate several AssetLinkedList objects and populate them from some csv files.
The key is for each AssetLinkedList object to intrinsically know which kinds of assets it will be filled with.
The following code compiles without errors but fails at runtime on line 164 (I've flagged in in the code with a comment ) with the message:
Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
So it seems to be pretty close. Any help/suggestions/hints/constructive criticism is always welcome. Like, is there a
better way for a instance AssetLinkedList to know what type of object it should contain without having to pass in an example
of the object?