Click to See Complete Forum and Search --> : Using an arraylist in Java


bwen
April 22nd, 2008, 12:29 AM
Hello,

I am needing to create a couple methods which link to an array list, I am given an input array list such as:

String[] array = {"Hello", "This", "This", "is", "is", "a", "is", "test", "test}

and the two methods I need to make are printWords() and topWords().

printWords() is suppose to list all the words and how many times they appear, so in used in conjunction with the above array list it should display:

Count - Words
1 ----- Hello
2 ----- This
3 ----- is
1 ----- a
2 ----- Test

It doesn't have to be in any particular order, and in topWords() I need the method to display the 2 or 3 (however many, does it make a difference how many it needs to display?) top showing words. So in this case it would be:

Count - Words
3 ----- is
2 ----- This
2 ----- Test

I know there is a better method using HashMap but the guidelines stipulate that I use an ArrayList, I have done it using a HashMap also but I'll post up the code for that later (I need help with that too)
Any help would be greatly appreciated, I'm not expecting anyone to write me any code but any help at all would be great, I'm an engineering student and just been hit in the face with some programming so I am in much need at the moment.
Many thanks,
Brendan

Londbrok
April 22nd, 2008, 01:28 AM
Great, so what is the difficulty you are having exactly? All so far is assignment specs.

bwen
April 22nd, 2008, 02:26 AM
Great, so what is the difficulty you are having exactly? All so far is assignment specs.
How I would tackle this question, with using a HashMap you have two columns allowing to store a value next to another value, in an ArrayList it's only one dimensional? I'm just not sure how to code it, basically I have an idea of how to do it, basically current string = string then plus 1 onto the counter and if current string != string then add it into the ArrayList and repeat for all the values given, is that the right way to do it?

I just don't know how to get it into Java code

_uj
April 22nd, 2008, 02:47 AM
I just don't know how to get it into Java code

Just introduce a simple class which holds a word and a counter, like

class WordCount {
String word;
int counter:
}

WordCount objects are stored in the ArrayList. The algoritm goes like the one for a HashMap. It's just that it takes longer because you got to search the ArrayList from the beginning to establish whether a word is already stored or should be added.

Note that the WordCount class can be refined in many ways. What I suggested is the bare minimum.

ProgramThis
April 22nd, 2008, 06:15 AM
with using a HashMap you have two columns allowing to store a value next to another value, in an ArrayList it's only one dimensional? Careful here, the "two" values that you are refering to in a HashMap are the Key and the Value. The Key is what determines the location that the value is stored in. So you aren't storing two values, you are using one 'value', the key, to store the other.

Google 'Hashing Java' to read up on how HashMap and hashing works in greater depth.