-
enumeration issues and assignments
Here is the syntax in question:
for (int startYear=initYear; startYear<initYear+3; startYear++) {
for (Enumeration e=buildingZone.keys(); e.hasMoreElements(); ) {
String buildingKey = (String)e.nextElement();
String zone = (String)buildingZone.get(buildingKey);
generateBuildingSummaryReport(startYear, buildingKey, zone);
}
}
I think I understand what hashtables are and that enumeration provides a
way to loop through a hashtable (in this case buildingZone is a hashtable).
nextElement() is the way enumeration goes through the keys located within
buildingZone. If I've got it pegged this far, then the question is, "Why
is (String) in front of e.nextElement() and why is (String) in front of buildingZone.get(buildingKey)?
Is this saying that whatever e.nextElement() finds we want to make it into
a string variable? I am confused as to what (String) is being used for here.
Thanks in advance for you help.
-
Re: enumeration issues and assignments
Yes, that's exactly what it's saying. The Java name for this is "class
casting", and there is a bunch of rules for how it works. You can't just
cast any class to any other class. So the programmer who wrote this code
would have known that the objects in the hashtable were strings. Since the
hashtable and enumeration are generic and widely usable, they can only deal
with Objects. And since all classes descend from Object, any object can be
cast into an Object variable.
So the phrase "(String)buildingZone.get(buildingKey)" means: "Get the
Object from the Hashtable buildingZone that belongs to the key buildingKey.
And I know it was a String when I put it there, so cast it into a String
variable."
But if it wasn't a String but some other class of Object, say a File or an
Integer, this will fail and a ClassCastException will be thrown at the
program.
Mark <msorteberg@inspec.com> wrote in message news:394a37d2@news.devx.com...
>
> Here is the syntax in question:
>
> for (int startYear=initYear; startYear<initYear+3; startYear++) {
> for (Enumeration e=buildingZone.keys(); e.hasMoreElements(); ) {
>
> String buildingKey = (String)e.nextElement();
> String zone = (String)buildingZone.get(buildingKey);
>
> generateBuildingSummaryReport(startYear, buildingKey, zone);
> }
> }
>
> I think I understand what hashtables are and that enumeration provides a
> way to loop through a hashtable (in this case buildingZone is a
hashtable).
> nextElement() is the way enumeration goes through the keys located within
> buildingZone. If I've got it pegged this far, then the question is, "Why
> is (String) in front of e.nextElement() and why is (String) in front of
buildingZone.get(buildingKey)?
> Is this saying that whatever e.nextElement() finds we want to make it
into
> a string variable? I am confused as to what (String) is being used for
here.
> Thanks in advance for you help.
-
Re: enumeration issues and assignments
Java is strongly typed language. So, you have to use casting. In fact all
you can have in container like hashtable is Object (with capital O) and nothing
more or less than that. Using (String) you cast your Objects into Strings.
You don't have to cast to String (of course if you don't need String) but
you have to cast to right type.
Try book from www.EckelObjects.com
It's free and excelent
Sorry for my bad English
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks