forEach and Casting
- From: Jason Cavett <jason.cavett@xxxxxxxxx>
- Date: 22 Apr 2007 10:32:22 -0700
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
.
- Follow-Ups:
- Re: forEach and Casting
- From: Tom Hawtin
- Re: forEach and Casting
- From: Tim Hemig
- Re: forEach and Casting
- Prev by Date: Re: TCP/IP file transfer and a router
- Next by Date: Bytebuffer to byte array and Object streams
- Previous by thread: TCP/IP file transfer and a router
- Next by thread: Re: forEach and Casting
- Index(es):
Relevant Pages
|