jdk 1.5 generics and return types

I

Ian

Hi,

I've got a question related to typing when using the new generics.

In the following, is there any way to specify the type of collection if
its known by the caller? Lets say the type of the objects to be return
is "Action".

static Collection getInstances(Collection<String> classNames,
Object ... args)
{
Collection instances = new ArrayList();
for (String className : classNames)
{
Object obj = getInstance(className, args);
instances.add(obj);
}
return instances;
}
 
E

E.Otter

Sure.... You have to remember that Collection is an interface so you have
to create an object of a class that implements the Collection interface in
some way. For example, create an ArrayList<Action> and then return the
reference to that.

static Collection<Action> getInstances(Collection<String> classNames, Object
.... args) {

ArrayList<Action> returnVals = new ArrayList<Action>();

... code that stores stuff in "returnVals" ...

return returnVals;
}

E.Otter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top