Have a look at HashMap to map words to lists of line numbers, and ArrayList to hold a list of line numbers. All the info is in the usual Java API docs.
JamesCherrill
Posting Genius
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
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)
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
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.
dononelson
Junior Poster in Training
62 posts since Mar 2010
Reputation Points: 13
Solved Threads: 15
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.
JamesCherrill
Posting Genius
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073