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.
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
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.
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
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 ]
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
posted
0
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 ]