I have a class Element which extends Node.
I would like to declare a List<> which should work like:
List<Node> elementsList;
for( Element e : (List<Element>) elementsList ) {
}
What kind of generic type should I use?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Declare it as a
List<Element>.If
Bar extends Foo, you cannot safely cast aList<Foo>to aList<Bar>.Alternately (NOT RECOMMENDED!):
but at that rate you might as well not use generics at all.