I have an input file that contains a bunch of records. Each record has a state abbreviation and several counts of various things.
My program is setup to read in the file and process the counts as a whole, and it works great. But what I need to do now is process the counts by state as well.
I figured that I would make a two-dimensional array, with the first dimension indicating the 50 states, and the second dimension to hold the accumulated counts for each state.
The trouble is, the state abbreviations are strings, and the counts are integers. I could probably use int constants to indicate each state, but I'm not sure if this is the easiest / most effective way to do what I want.
If you are going to have to trawl the array to find the matching state for every record you read in you'd be better off using a Map such as HashMap. Maps are much more efficient at finding data than having to search an array each time. You could use the state string as the key and create a simple local mutable integer class (to hold the count) as the value.
Joined: Sep 07, 2007
I've never used a HashMap before... can someone show me how to create a simple one?
I think I would recommend making an enum, and using an enum map. Although 50 is awefully big for an enum, it'll make your code cleaner, because if you find a name isn't a state, it'll get caught by the valueOf method. This should be relatively safe, because the fifty states aren't likely to change anytime soon.