Cast element in Java For Each statement

Posted by Carl Summers on Stack Overflow See other posts from Stack Overflow or by Carl Summers
Published on 2010-03-19T20:14:37Z Indexed on 2010/03/19 20:21 UTC
Read the original article Hit count: 280

Filed under:
|
|

Is it possible (or even advisable) to cast the element retrieved from a for each statement in the statement itself? I do know that each element in list will be of type .

I.E.:

List<BaseType> list = DAO.getList();  
for(<SubType> element : list){ 
    // Cannot convert from element type <BaseType> to <SubType>
    ...
}

rather than:

List <BaseType> list = DAO.getList();
for(<BaseType> el : list){
    <SubType> element = (<SubType>)el;
    ...
}

© Stack Overflow or respective owner

Related posts about java

Related posts about type-casting