Sarah Evans wrote:I believe my problem is with the way I'm trying to add the words to the key if the key already exists, because the first entry is always a string but after that it's not.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Do you really mean LinkedHashTable? LinkedHashMap, surely?
Now, yes, you are creating a new ArrayList every time (temp5) and adding it to the existing list if there is one. What you want to do is only create a new list only if there isn't one already in the hash for your key, and then put it into the hash. If there is a list in the hash already, you want to get it from the hash, and add your new element to it. You don't even have to put it back in the hash, because it's already there.
Sarah Evans wrote:Sorry, I did mean LinkedHashMap not LinkedHashTable! I've been at this all day trying to figure this stuff out so I guess my brain is fried.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
OK. So let's back up.
Forget HashMap's and ArrayList's - What are you trying to do?
You've run into one of the quintessential things about the business of programming: Code will never, EVER solve your problem.
You need to understand - and be able to describe to a dummy like me - WHAT it is you want to do, before you write your first line of Java code.
So: What do you want to do; and what made you think that a HashMap was the way to solve it?
Then we'll get down to the business of what's wrong with your code.
I ask again: What are you trying to do? Explain it to me, and explain the reasons for your choice of classes
- and if you were told to use them, then tell me why you think you were told to use them.
You must understand the problem, Sarah; otherwise you'll simply get more and more frustrated.
Sarah Evans wrote:I was wanting to use a HashMap because that was the only data structure that I felt comfortable with that I thought I could use to associate what lines of text belonged to what documents.
The whole point of this program is to build a inverted index, where the hashmap reveals which documents the words can be found in.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
HashMap<String, ArrayList<Integer>>
would seem like a better choice.
Will you ever clean it up later? Get it right first time and it will be a lot quicker.Sarah Evans wrote: . . . the poor variable naming. . . . clean it up later . . .
Sarah Evans wrote:Now for my second HashMap. I have encountered a error and I don't know why it is happening.
I traced through the loops multiple times with the debugger, and I still can't figure out what's causing it. Any ideas?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
I think you would end up writingSince ArrayList implements the List<E> interface, you can put an ArrayList as a “V” in a Map<Foo, List<E>>.Sarah Evans wrote:
HashMap<String, ArrayList<Integer>>
would seem like a better choice.
I didn't know you could do that. . . .
Earlier, I wrote: . . . “get”ted from a Map. . . .
No, it wasn't until after that point that I saw the code in its full glory and realised how long it was. I thought it looked confused because of the peculiar variable names.Winston Gutkowski wrote: . . . As I believe Campbell already pointed out, . . .
Sarah Evans wrote:I'm so happy that this is finally done. I've learned SO much.
Thank yoooooou
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Don't get me started about those stupid light bulbs. |