• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help building and returning a Map or an Array  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done lots of research and reading on these forums and have found helpful advice but this is the first time I've needed to post for help. We'll add this to the hordes of questions on Huffman programs. I have a class that successfully builds a Huffman tree and generates the code. What I'm having trouble with is returning an Array or a Map of those codes. Almost all of the examples I've seen are about printing the codes which I can do successfully but what would help me, is how to build a map of the codes and return them. I'm sure I'm missing something simple but this has me at my wits end. I'll post what I have now. I have tried to create an ArrayList and a TreeMap and insert the values of the character and code but when I return them I get empty data.

If it matters this is for an assignment but I've done what I think is the hard part I feel like I'm stuck on something very simple.

Any help would be appreciated.

 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming this code is correct, how are you trying to build your ArrayList or Map?
 
Deron Fuller
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I had tried to add to build the Map was an alternate of the genCode() method I have above and add the 2 methods to access it. It compiled but passes an empty map. I know I'm having a problem initializing and saving the map correctly but I'm a little lost as to where. This also touches on the idea of searching the tree. I can search a tree that's sorted on values but this is created using a priority queue which sorts by frequency which makes it a little harder for me to figure out a good way to search it and return the value but that won't matter if I can simply pass back an array or map of the characters and their codes.

 
Deron Fuller
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured out my problem. I get lazy with variable declaration and I simply made my Map static and it returned correctly. I think some experienced java programmers might frown on that because of the extra memory allotted but it worked for me.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!