问题在Arraylist中的count字段并保存 [英] Problem In count field in Arraylist and save it

查看:23
本文介绍了问题在Arraylist中的count字段并保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有一个带有这个值的 ArrayList

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

  • 我们将获得 4 个 ACU,
  • ACY 我们将得到 2,
  • AER 我们将得到 3 个,
  • AGC 我们将得到 1.

一般一个词重复的次数是一个变量所以另一次 ACU 可能是 1,而 ACY 可能是 100..

Generally the number a word is repeated is a variable so another time ACU could be 1,and ACY could be 100..

所以,我有一个类来保留值whatWord"和howMany"

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;
 } 
}

我被困在这里,因为我知道下面代码中的 get(i+1) 部分会导致错误,您知道值不存在,但我不知道该怎么办...

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)
      }
        }

应该是在for代码之后我会得到

It is supposed that after the for code I will get

ACU 4,
ACY 2,
AER 3,
AGC 1.

首先我尝试做一个 HashMap 尝试,请帮我解决这个代码:

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);
         }

提前致谢.

推荐答案

这对于您想要做的事情来说可能有点过头了.您可以更简单地创建一个以三个字母的字符串为键、以计数为值的映射.然后只需遍历您的 ArrayList:

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);
}

这篇关于问题在Arraylist中的count字段并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆