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<Orig开发者_JAVA百科inalList.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.
Iterate over OriginalList
and put the word in a HashMap<String, Integer>
. If it is not there start with count 1
, otherwise increment it.
That might be overkill for what you want to do. You could more simply create a map that has the three-letter string as the key and the count as the value. Then just iterate over your ArrayList:
Map<String, Integer>wordCount = new HashMap<String, int>();
for(String seq : yourWordList){
// increment the count of the word by first obtaining its count,
// and then incrementing it. Paranthesis for clarity
wordCount.put(seq, (wordCount.get(seq)) + 1);
}
I think the answer your looking for is common-collections' CollectionUtils.getCardinalityMap().
JavaDoc: Returns a Map mapping each unique element in the given Collection to an Integer representing the number of occurrences of that element in the Collection.
So for example....
List<String> words = new ArrayList();
Map counts = CollectionUtils.getCardinalityMap(words);
Why not set your loop counter at 1?
String word1 = OriginalList.get(i-1).getWord();
String word2 = OriginalList.get(i).getWord();
This way, you never run out of bounds.
精彩评论