This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Storing a count in a || array Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Storing a count in a || array" Watch "Storing a count in a || array" New topic
Author

Storing a count in a || array

michael bradly
Ranch Hand

Joined: Oct 06, 2000
Posts: 112
I'm working on a program that reads from a text file, parses each word and sorts them into an Array Based List. In addition I need a parallel array to keep track of how many times a word appears in the text file. So if a word appears more then once, it only goes into the SortedArrayBasedList once and its count is incremented in the parallel array.
I'm having a hard time conceptualizing how to maintain the second array in relation with the first.

After I add the word to my sorted list, I'm a bit befuddled as to how to handle a check to a second array in relation to the first. Any suggestions would be appreciated.
Regards, Michael
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 195
Well,
the answer is kind of depend on how your
SortedArrayList behave. If it implements
List, it means it accept any object type..
so it's easier to create a wrapper object that
hold your String and the count.
Or you can used the indexing info as your
cross reference to the second array.
Hope that's help.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Why don't you use one of the Maps to hold your data?
Your words would be the keys and the counts would be values. The counts would need to be simple objects having an int attribute (counter) and a method to increment that attribute.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
michael bradly
Ranch Hand

Joined: Oct 06, 2000
Posts: 112
So the idea behind this would be something like

aList.sortedAdd(word); when word is "then"
if this word matches a word in aList
then aList.getIndex(word)
Then use this index in the second array to increase the count...
if the word is "yet" which doesn't exist
aList.sortedAdd(word);
aList.getIndex(word);
and add 1 to that index in the second array..
And in this type of situation, as the SortedList shifts, I would have to shift the corresponding items in the second array as well? Or is there an easier way of maintaining some handle on the second array without having to shift that as well?
Thanks for the help, Michael

Originally posted by Ryo Saeba:

Or you can used the indexing info as your
cross reference to the second array.
Hope that's help.

[ February 27, 2003: Message edited by: michael bradly ]
[ February 27, 2003: Message edited by: michael bradly ]
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
hi
as mentioned before in this thread, i would
use a HashMap to do this.

this should give you an idea ... the code is neither compiled nor double checked ;-)

k
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Karl, rather than using an Integer I would use
a class like:

Then I would do: ((Counter)map.get(word)).bump();
I would also use a java.util.TreeMap to do the trick, to save bother of sorting on words at the end. Sorting on word frequeuency is interesting but we'll leave that for now (that is implementing equals() and hashCode() )
-Barry
[ February 27, 2003: Message edited by: Barry Gaunt ]
[ February 27, 2003: Message edited by: Barry Gaunt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Storing a count in a || array
 
Similar Threads
read words from file into array
Scanning a text file
Text Formatting Question
Reading words in a phrase without Tokenizer
help with counting operands and operaters