Results 1 to 8 of 8
Thread: Sort Frequency
- 03-25-2009, 03:34 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
Sort Frequency
I need to read in a file, store the words in an arraylist and sort it in order to the number of times it appears in arraylist, starting with the most frequent. What is the easiest way to do this only using an arraylist or an iterator?
I needed to create two classes:
Word class contains a getWord, getCount, addOneToCount, and toString
WordList class uses an ArrayList of Word class (not inherit), contains an addWord method, getSize, search, sort, print, and toString.
I need help writing the sort and the print. In my main, i get a foreach error for the print.
Someone please help me!!! :(
Thank You
- 03-25-2009, 03:56 AM #2
Arrays.sort(yourArray);
I use this to sort an int array. Works beautifully.
For printing the array...
for(int i=0;i<yourArray.length();i++){
//Print to Output, File, etc.
System.out.println(YourArray[i]);
}
Internet Search is a vital part of learning/ figuring out how to make things work...
Search = Java Sort Array
Search = Java print array
Etc...
- 03-25-2009, 03:59 AM #3
Opps, should've read the post more carefully. Your looking for sorting according to parameters. I would do a for loop, iterate through the array, count the number of times a variable exists and store the counts, then you can print accordingly or store according ly.
- 03-25-2009, 06:08 PM #4
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
for (WordList list : myList)
{
System.out.println(list);
}
I get a foreach not applicable to expression type message with this. I have used this code similarly with other variable names and applications but it will not work with this one.
Also, can anyone help me with the sort? I am sorting Strings by how frequent they appear in a text document
Thank You
- 03-25-2009, 09:23 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
This code assumes that myList is a array (or List) with WordList elements: ie that it is a list of lists. It's quite likely that this is not the case and that you mean something else.Java Code:for (WordList list : myList) { System.out.println(list); }
If your question is about how you would print the elements of some array or collection, post the brief but compilable code you would use for this. If this code results in a compiler message that you can't understand, post the actual message.
- 03-25-2009, 10:19 PM #6
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
parse your text into a hashmap with key being the word and value being the count. it's all downhill from there.
- 03-25-2009, 11:34 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
I agree that a Map would make it straight forward but, alas, from the original post "What is the easiest way to do this only using an arraylist or an iterator?"parse your text into a hashmap with key being the word and value being the count.
- 03-26-2009, 12:47 AM #8
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
Similar Threads
-
Using Merge Sort to sort an ArrayList of Strings
By coldfire in forum New To JavaReplies: 3Last Post: 03-13-2009, 01:03 AM -
Word Frequency
By capu in forum Advanced JavaReplies: 2Last Post: 10-09-2008, 02:03 PM -
Frequency Counter
By justlearning in forum New To JavaReplies: 0Last Post: 05-07-2008, 10:50 PM -
[SOLVED] BST Frequency Counter
By theonly in forum Advanced JavaReplies: 7Last Post: 04-29-2008, 11:33 PM -
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 08:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks