In Class1 we have a method doGet
and a method that returns a List with objects whom type is Animal.
public List<Animal> getAnimals()
so I call method like:
List<Animal> animal = getAnimals();
request.setAttribute("animal_list", animal);
In Class 2 i have a method doGet too, and i want to take animal_list. My code is :
List<Animal> list500=request.getAttribute("animal_list");
but when I compile I get the message
"Object cannot be converted to List<Animal>"
a cast from Object
to List<Animal>
is necessary here:
@SuppressWarnings("unchecked")
List<Animal> list500= (ArrayList<Animal>)request.getAttribute("animal_list");
request.getAttribute
returns just Object
so you have to define the exact type via cast
. Object
could be everything...
as a side node: you will get a
"Unchecked cast from Object to ArrayList"
warning from the compiler
. In this case you can add
"suppress the warning"
annotation
. After that the warning will disappear.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.