Hi, well, I have an ArrayList with this values
ACU
ACU
ACU
ACU
ACY
ACY
AER
AER
AER
AGC
I need to get the number of items of each Word, so for
- ACU we will get 4,
- ACY we will get 2,
- AER we will get 3,
- AGC we will get 1.
Generally the number a word is repeated is a variable so another time ACU could be 1,and ACY could be 100..
So., I have a class to keep the values "whatWord" AND "howMany"
public class Word {
private String whatWord;
private int howMany;
public cVOPlaza(String whatWord, int howMany){
this.whatWord = whatWord;
this.howMany= howMany;
}
public String getwhatWord() {
return whatWord;
}
public void setwhatWord(String whatWord) {
this.whatWord = whatWord;
}
public int gethowMany() {
return howMany;
}
public void sethowMany(int howMany) {
this.howMany = howMany;
}
}
I am stuck here, because I know the part get(i+1) in the following code will cause error, You know value does not exist, but then I dont know what to do...
ArrayList<Word> arrayWords = new ArrayList<Word>();
int cuantos = 0;
for (int i=0;i<OriginalList.size();i++) {
String word1 = OriginalList.get(i).getWord();
String word2 = OriginalList.get(i+1).getWord();
if (word1.equals(word2)){
//this counter is bad here...
//where do i increment it??
howMany++;
Word a = new Word(word1,howMany);
///....DONT KNOW WHERE TO ADD THE OBJECT
//to the list
//arrayWords.add(a)
}
}
It is supposed that after the for code I will get
ACU 4,
ACY 2,
AER 3,
AGC 1.
First I tried to do a HashMap try, please help me with this code:
HashMap table = new HashMap();
int value=0;
String key=null;
//INITIALIZE HASH??? LIKE THIS
for (int i = 0; i < OriginalList.size; i++) {
table.put(0,OriginalList.get(i).getWord());
}
String word1=null;
ArrayList<Word> arrayWords = new ArrayList<Word>();
//LOOP FOR SEARCHING
for (int i = 0; i < OriginalList.size(); i++) {
key = OriginalList.get(i).getWord();
if (table.containsKey(key)) {
word1 = (String) table.get(key);
value++;
}else {
word1 = (String) table.get(key);
value=1
}
//Add result??
Word a = new Word(word1,value);
}
Thanks in advance.