Results 1 to 13 of 13

Thread: Table Data Type

  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default Table Data Type

    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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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>.
    Last edited by Fubarable; 03-14-2011 at 03:14 AM.

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    not an ADT, sorry. The characters and their counts will be kept in a table data structure.

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    so the characters would be the 'keys' of the map?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sehudson View Post
    so the characters would be the 'keys' of the map?
    Yes. And the value would be the count of occurrences.

  6. #6
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    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?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  8. #8
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    hmm.. looks like get is returning an object. Is it as simple as casting it to an int?

  9. #9
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    nevermind, I see what what you are saying

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sehudson View Post
    hmm.. looks like get is returning an object. Is it as simple as casting it to an int?
    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.

  11. #11
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    so it would be like:

    HashMap table = new HashMap<Character,Integer>;

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sehudson View Post
    so it would be like:

    HashMap table = new HashMap<Character,Integer>;
    No, and you might want to have a look at the generics tutorial in the Oracle Java tutorials. The HashMap declaration, the left hand side of this expression is also generic.

  13. #13
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    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

Similar Threads

  1. Matrix Data Type
    By fraB3422 in forum New To Java
    Replies: 7
    Last Post: 02-27-2011, 03:08 AM
  2. Understanding limitations of int data-type
    By JONCOM in forum New To Java
    Replies: 11
    Last Post: 01-30-2011, 10:57 PM
  3. Replies: 0
    Last Post: 12-03-2010, 07:07 AM
  4. how to set NVarchar data type
    By edi.gotieb in forum JDBC
    Replies: 9
    Last Post: 05-19-2010, 02:13 PM
  5. sychronized data type
    By java girl in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-13-2009, 09:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •