• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with HashMaps

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there, I am looking for help with HashMaps.

I got the following code for initializing a HashMap (which is for a language recognizing program). The first HashMap contains the language as key (String), and another hashmap as value. The second hashmap has the letters (in ASCII) as key, and a Vector as value. The Vector will hold the amount of occurences of each letter. I am asking if this is the correct way to initialize it:



And is this the correct way of adding new languages and accessing the components in that kind of HashMap structure?

Also I'd like to ask if there are any alternative ways of achieving the same kind of linked structure?

Cheers,

Michael
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a similar problem.... I can't find the thread now, but I think the way I fixed the compiler issue I was getting is to get rid of the internal generic declaration on the right side of the expression.

The compiler just cares what the list or map is populated with. If it's populated with lists or maps, great... but it doesn't need to know what the internal list or map has in it.

so this won't work:

But this should:


I think the reasoning for this is the way the declarations of generics work and where they're needed...

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and Welcome to JavaRanch
 
Michael Duff
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, and thanks Janeice, think that makes the structure a bit clearer.

But now I face another problem that is Vector, is there any way to make the indexing of the Vector start from something else than 0? I am looking for something like this:



Where count is the amount of a specific character in a text, and percent is the percentage a specific character represents in a text. All I'm getting is IndexOutOfBoundsExceptions... and I seem to be very poor at finding information from internet because I couldn't find anything for this exact problem.

Cheers,

Michael
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm... I don't know much about vectors.... why don't you start a new thread? You'll probably get more responses that way.

And you're welcome!
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not a great use of a Vector. A Vector is basically just a resizeable array. It sounds like you just want another map, using character as a key and a statistical object as the value. Consider making a simple class to hold the data you want and putting that into your map. Like:


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic