J
Jason Cavett
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
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