I'm working on a small project and have run into some problems. Briefly described, what I'm attempting to do is:
I've a rather large text file with a different sentence and I need to find all the words in each sentence and add them to some sort of index, so that the structure is: key = word and value = line number(s). For example like this:
James' solution is the one that I would have suggested. It's a good one if the file is pretty static, which is what I think you're describing.
It occurs to me, though that if the file is dynamic it's a bit hard to keep your lists correct. If the application is a word processor, for example, as soon as someone deletes a sentence on line 57, everything following is out of synch. I'm not sure what I'd do in that situation, but I think recalculating that list each time the file changes would be computationally expensive. That's an interesting problem to consider, and you might learn some useful things by trying to solve it. (So might I, and I think I might)
thank you for your response. I'm certainly trying to follow the suggestion made my you and James with the Hashmap and ArrayList.
You are right that it is static files I'm dealing with (right now anyway), but you are right that it could raise a potential problem and thus a interesting puzzle to solve how to deal with an dynamically changed file.
I think though that I'll start on concentrating on the static file in first round.
Just as a matter of convenience, there is a LineNumberReader available that will keep track of what line you are on - not sure if you need it, you may be just keeping your own counter, but I thought I'd bring it up.
The good news is that if you need to change to some other means of tracking the lines / locations you will just change the Objects stored in the ArrayList; the HashMap side of things shouldn't be affected.
In fact, if I thought that was likely to happen I'd start out from day 0 with a class (eg Location) that, in its first iteration just holds a line number, but could always be improved.