Re: Make this 'generic'
Sandy wrote:
I do still however want to know if I can, then how I can, tern this
class into something that can put any other class into the vector (or
arrayList if I convert it) rather than me re-writing it for each class
simply changing the name of the class that is going into the vector
(or arrayList) and the name of the class of what is coming back out.
Please bear with me while I take a step back and try to get a wider view of
the problem.
When you say "put {a] class into the Vector" (spelling is important), do you
literally mean to store the class in the collection, or an instance of the class?
You can do either, sort of. Actually, you can only ever store objects, but
there is an Object called Class<T> that will let you do "classy" things. Since
we want anything to be the type of the class, we'll use the wildcard ? for the
generic type. (Incidentally, I am still a bit weak on generics, so anyone jump
in with corrections.)
Let me use ArrayList, but all we really care about is that it's a List - any
kind of List will do, won't it?
List <Class <?>> classes = new ArrayList <Class <?>> ();
Now you have a list of Class objects.
But what if you just want a list of objects, and what you really meant is that
you don't care what type of Object they are?
Then you can use List <Object>.
List <Object> stuff = new ArrayList <Object> ();
You can now have anything you want in the list, but something that retrieves
from the list will have to take steps to uncover the actual runtime type.
Does this bear on your question?
By the way, if what you want is just a collection of stuff that only has one
of any given thing in it, no duplicates, use Set instead of List. Also, the
code you posted just duplicated collection class methods. Why not use, say,
HashSet directly instead of writing a class to do what it already does?
-- Lew