I am reading in a file char by char and keep a count of the number of occurences of each letter, and need to come up with a way to save the data in a Table ADT. I didnt think there was a table data type. How can I represent the data as a table?
Printable View
I am reading in a file char by char and keep a count of the number of occurences of each letter, and need to come up with a way to save the data in a Table ADT. I didnt think there was a table data type. How can I represent the data as a table?
What is a "Table ADT"? What specifically are your requirements?
Edit: For what it's worth, normally, I'd store this frequency data in a HashMap<Character, Integer>.
not an ADT, sorry. The characters and their counts will be kept in a table data structure.
so the characters would be the 'keys' of the map?
Ok, so if I do a myHashMap.put(A,1) for the first time I find an A, how would I update the value portion (the 1) the next time I find an A?
First you'd call get on the HashMap using your char as a key. If it returns null, then you know that this is the first time that this char was encountered and you'd put in a 1 for this char. Otherwise if it returns a viable Integer, you get the intValue, add one to it, and put the resulting incremented value.
hmm.. looks like get is returning an object. Is it as simple as casting it to an int?
nevermind, I see what what you are saying
No, try it and you'll see what happens if you try to cast a reference type to a primitive. You should be using a generic HashMap<Character, Integer>. So get will return an Integer. You get an int from this by calling intValue on the Integer returned, or you could just use it as is via auto-unboxing.
so it would be like:
HashMap table = new HashMap<Character,Integer>;
Ok..now I have to add the characters into a 'min heap' according to frequency? I guess I can iterate over the HashMap, but I have not worked with a min heap