Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > jdk 1.5 generics and return types

Reply
Thread Tools

jdk 1.5 generics and return types

 
 
Ian
Guest
Posts: n/a
 
      07-25-2005
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;
}

 
Reply With Quote
 
 
 
 
E.Otter
Guest
Posts: n/a
 
      07-26-2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generics in return types kelvSYC Java 2 12-13-2007 05:43 PM
What is the difference between J2EE, JDK, JDK-SDK, JRE and J2SE packages ? Ulf Meinhardt Java 0 08-10-2006 07:12 PM
Generics? Covariant return types? Webb Roberts Java 3 11-10-2005 07:21 AM
Covariant Return Types in 1.5 - doesn't work with Generics? Robert Elliot Java 10 06-06-2004 09:47 PM
Boost + Python C/API: Mixing python return types with boost return types Steve Knight Python 2 10-10-2003 10:11 AM



Advertisments