forEach and Casting



I have a question about the use of the foreach statement in Java 1.5.

Currently, the collection I'm using an Iterator over contains objects
of a Generic type (an abstract class). When I get the object from the
collection, I must cast it to the more specific type so I can use the
various methods. There is no way to change this.

Is there any way, using the foreach statement in place of using an
Iterator, to cast to the right type? I'm thinking something like
this...

Collection<Generic> genericCollection = new ArrayList<Generic>();
genericColelction = doSomeStuffToGetGenericCollection();

// this is the part I'm not sure about - can I even do something like
this?
for(Specific o : (Specific) genericCollection) {
// do stuff
}


Thanks

.



Relevant Pages

  • Re: forEach and Casting
    ... the collection I'm using an Iterator over contains objects ... to cast to the right type? ... genericColelction = doSomeStuffToGetGenericCollection; ... Everything, that gets overloaded is useable, because the abstract class ...
    (comp.lang.java.programmer)
  • Re: forEach and Casting
    ... to cast to the right type? ... genericColelction = doSomeStuffToGetGenericCollection; ... Everything, that gets overloaded is useable, because the abstract class ... public ArrayListgetDirectDescendentsOfType(Class classType) ...
    (comp.lang.java.programmer)