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.

Thanks in advance.

use a hashmap with the words as keys and the counts as values.
If you find a word in it, increase the count.
If not, add it with a count of 1.

Comments
As Ezzaral has pointed out, this is a correct solution. My apologies!
Agreed. Good suggestion.

1. get all input and store it ad ArrayList

2.then copy arraylist in HadhMap and iterate it and compare it..

3.while comparing you remove matched item cause next time it will not come again...

4.the problem here is you need to count and put the final count for corresponding variable...so u do it by get unique variable and store it in another hashmap...

5.again Iterate that hashmap and and match final count as key and variable as pair...

can u get it?...

Ok, Thanks for the replies, but well, I guess I do not know to much about hash tables??

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

Please any help is very much apreciated

I have to disagree with jwenting here. If you are already using an ArrayList then why use an additional data type that doesn't even help you in any way?

Do the following:

1. Write a for loop that iterates over your entire array.
2. For each element in the array, test if it is equal to the thing you want to compare it to (your word) using the equals method. *
3. If they are equal, increment a count.

* Since you have a String inside of a Word Object, the way you'd use the equals() method is as follows:

String currentWord = arrayWords.get(i).getWhatWord();
if (currentWord.equals(wordYourLookingFor)){
//do stuff
}

Also, you seem to be confused, because your variable is named whatWord. In one case, you want to search for a particular word to see how many times its in the array. But why don't you just made your ArrayList an ArrayList of Strings instead of one of Words?

Edited 6 Years Ago by BestJewSinceJC: n/a

Hi, well thanks for your reply,
the question you make:
But why don't you just made your ArrayList an ArrayList of Strings instead of one of Words?

The answer is that I need to have an ArrayList of Words with their respective ocurrence because I will need them later, for access in a query,

Also I was trying to do the map thing but still dont get it right:

int value=0;
Map<String, Integer>wordCount = new HashMap<String, Integer>();
for(String seq : WordList){
      Set<String> set = wordCount.keySet();
      value = set.size();
      wordCount.put(seq, value));
}

But I get other numbers..

Please help me in this one

ACU 0
ACU 1
ACU 1
ACU 1
ACY 1
ACY 2
AER 2
AER 3
AER 3

I am not getting
ACU 4
ACY 2
AER 3....

Edited 6 Years Ago by endsamsara: CODE

If you want help doing it by using a HashMap you'll have to wait for jwenting. I don't understand why a HashMap would be useful for your purposes at all. It seems to be redundant to me, jwenting will have to explain why he suggested that (he may have had good reason, I just don't see it if there is one). And I already told you in my previous post how to do it using your current ArrayList. Simply put the code I provided in a nested for loop, and count the number of times you see each item, and you are done. You hardly have to do anything.

Edited 6 Years Ago by BestJewSinceJC: n/a

check it...

import java.util.*;
public class RemoveDuplicate {

/** Creates a new instance of RemoveDuplicate */
public RemoveDuplicate() {
}

@SuppressWarnings("unchecked")
public static void main(String args[])
{

ArrayList arlList = new ArrayList();
arlList.add("ACU");
arlList.add("ACU");
arlList.add("ACU");
arlList.add("ACU");
arlList.add("ACY");
arlList.add("ACY");
arlList.add("ACY");
arlList.add("AER");
arlList.add("AER");
arlList.add("AGC");
removeDuplicate(arlList);


}
@SuppressWarnings("unchecked")
public static void removeDuplicate(ArrayList arlList)
{
HashSet h = new HashSet(arlList);
//arlList.clear();
ArrayList arlList1 = new ArrayList();
arlList1.addAll(h);
System.out.print("arlList : " + arlList1);
ArrayList arlListfinal = new ArrayList();
for(int i=0;i<arlList1.size();i++)
{
	String val=arlList1.get(i).toString();
	int k=0;
	for(int j=0;j<arlList.size();j++)
	{
		
	if(arlList1.get(i)==(arlList.get(j)))
		{
		
		
			k++;
			
		}
	}
	arlListfinal.add(val+k);
}
for(int x=0;x<arlListfinal.size();x++)
{
	System.out.println(arlListfinal.get(x));
}
}
}

Edited 6 Years Ago by musthafa.aj: n/a

Though this may not be the best solution, you can also achieve it as follows,

import java.util.*;
class Word {
 
    private String whatWord;
    private int howMany;
 
    public Word(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;
	}



	//Overriding the equals() and hashcode() methods so that Word objects with equal whatWord value are same
	public boolean equals(Object obj) {
          if(this == obj) {
                return true;
           }

           if (!(obj instanceof Word)) {
                  return false; 
           }

           Word word = (Word)obj;
           return whatWord.equals(word.getWhatWord());
 
    }


	public int hashCode () {
		int hash = 7;		
		hash = 31 * hash + (null == whatWord ? 0 : whatWord.hashCode());
		return hash;
    }
}


public class WordCounter {
 

 
	public static void main(String args[]) {
 
	ArrayList<Word> arlList = new ArrayList<Word>();
	arlList.add(new Word("ACU", 0));
	arlList.add(new Word("ACU", 0));
	arlList.add(new Word("ACU", 0));
	arlList.add(new Word("ACU", 0));
	arlList.add(new Word("ACY", 0));
	arlList.add(new Word("ACY", 0));
	arlList.add(new Word("AER", 0));
	arlList.add(new Word("AER", 0));
	arlList.add(new Word("AER", 0));
	arlList.add(new Word("AGC", 0));
	countWords(arlList);
}



	public static void countWords(ArrayList<Word> arlList){
		//iterate the arraylist and calculate the frequency of the given objects
		for(Word currWord : arlList){
			int count = Collections.frequency(arlList, currWord);
			currWord.setHowMany(count);
		}

		
		//Now remove the duplicate records from the arraylist
		LinkedHashSet h = new LinkedHashSet(arlList);
		arlList.clear();
		arlList.addAll(h);

		//diplay the list
		for(Word currWord : arlList){
			System.out.println(currWord.getWhatWord() + "=" + currWord.getHowMany());
			
		}
	}

}

Edited 6 Years Ago by Nick Evan: Added code-tags

Thank you very much, your help is very much apreciated :)

check it...

import java.util.*;
public class RemoveDuplicate {

/** Creates a new instance of RemoveDuplicate */
public RemoveDuplicate() {
}

@SuppressWarnings("unchecked")
public static void main(String args[])
{

ArrayList arlList = new ArrayList();
arlList.add("ACU");
arlList.add("ACU");
arlList.add("ACU");
arlList.add("ACU");
arlList.add("ACY");
arlList.add("ACY");
arlList.add("ACY");
arlList.add("AER");
arlList.add("AER");
arlList.add("AGC");
removeDuplicate(arlList);


}
@SuppressWarnings("unchecked")
public static void removeDuplicate(ArrayList arlList)
{
HashSet h = new HashSet(arlList);
//arlList.clear();
ArrayList arlList1 = new ArrayList();
arlList1.addAll(h);
System.out.print("arlList : " + arlList1);
ArrayList arlListfinal = new ArrayList();
for(int i=0;i<arlList1.size();i++)
{
	String val=arlList1.get(i).toString();
	int k=0;
	for(int j=0;j<arlList.size();j++)
	{
		
	if(arlList1.get(i)==(arlList.get(j)))
		{
		
		
			k++;
			
		}
	}
	arlListfinal.add(val+k);
}
for(int x=0;x<arlListfinal.size();x++)
{
	System.out.println(arlListfinal.get(x));
}
}
}

Thank you, I like how you used the theory and prime number.

Excellent.

Thanks!!:-O

Edited 3 Years Ago by Nick Evan: Fixed formatting

The fact that people would just post complete answers to a problem like this is just insulting to me. I don't know about some of the other legitimate posters here, but I make an attempt to give people enough help so that they can learn things without giving them a cookie cutter solution.

And that's why I won't provide a complete solution either, which is what would show you why using a Map here is the best choice.
It's far more elegant, probably faster, and being more concise is less likely to contain errors.

I agree with jwenting that a map is great for this. He already gave precise instructions on how to process the list. The rest is simply a matter of looking at the Map api for the details on the required methods. The actual code to do it is only a couple of lines longer than his description.

Edited 6 Years Ago by Ezzaral: n/a

This question has already been answered. Start a new discussion instead.