arraylist - Typecasting Elements in java.util.List -



arraylist - Typecasting Elements in java.util.List -

i have 2 classes named "basecatalog" , "city". "basecatalog" parent class of "city", in other words, "city" extends "basecatalog" class.

i have method called "getbasecatalogobjects" takes 1 parameter catalogtype , returns list of basecatalog objects. according given type, type of list may differ contain objects extends main basecatalog class.

because method generic method, produces list containing basecatalog objects. in case, sure each basecatalog instance in list city object:

list<basecatalog> basecatalogobjects = getbasecatalogobjects(catalogtype.city);

what want convert "basecatalogobjects" list list called "cityobjects" like:

list<city> cityobjects = (list<city>) basecatalogobjects;

is possible or there efficient way such conversion without creating new java.util.list instance , iterating on list "basecatalogobjects"?

as suggested peter can alter method signature follows,

public <t extends basecatalog> list<t> getbasecatalogobjects(class<t> clazz, catalogtype type){}

and can cast list in homecoming statement like,

(list<t>) list;

hope helps!

java arraylist type-conversion extends

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -