The Artima Developer Community
Sponsored Link

Java Answers Forum
Sets

2 replies on 1 page. Most recent reply: May 3, 2003 11:54 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Duane

Posts: 5
Nickname: duane
Registered: Mar, 2003

Sets Posted: May 3, 2003 7:41 AM
Reply to this message Reply
Advertisement
I need to compare words in a set and then display the number of occurances of each word.
Any ideas will be appreciated.


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Sets Posted: May 3, 2003 11:33 AM
Reply to this message Reply
In java, a Set is a collection that contains no duplicate elements. Thus if a word is contained in a set, then it occurs only one time.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Sets Posted: May 3, 2003 11:54 AM
Reply to this message Reply
Assuming you did not mean set as a java.util.Set check out the following:

/**     Uses an ArrayList to contain a collection 
 *      of String words.
 */
public class WordList extends java.util.ArrayList{
 
    /** Constructs an empty Wordset.
     */
    public WordList(){
        super();
    }
    
    /** Constructs a WordList from an array of 
     *  word string objects.
     */
    public WordList(String[] words){
        super();
        for (int i = 0; i < words.length;i++){
            add(words[i]);
        }
    }
        
    /** Counts the number of times the input string matches 
     *  exactly each word in the ArrayList
     */
    public int getOccurances(String word){
        int occurances = 0;
        java.util.Iterator iterator = iterator();
        while (iterator.hasNext()){
            String nextWord = (String)iterator.next();
            if (nextWord.equals(word)) occurances++;
        }
        return occurances;
    }
    
    /** Determines if the input string matches 
     *  at least one word in the ArrayList.
     */
    public boolean occurs(String word){
        return contains(word);
    }
    
    /** Returns the String array of words.
     */
    public String[] getWords(){
        String[] strings = new String[size()];
        for (int i = 0;i < size(); i++){
            strings[i] = (String)get(i);
        }
        return strings;
    }
 
    /** Returns the array of words as a List.
     */
    public java.util.List getList(){
        return (java.util.List)this;
    }
    
    /** Sorts the underlying an array of word 
     *  string objects using the static
     *  Arrays.sort(String[]) method.
     */
    public void sort(){
        java.util.Arrays.sort(getWords());
    }
 
}


The following class tests the feature you wanted.

public class Test{
    public static void main(String[] args){
        new Test().test();
    }
    
    public void test(){
        WordList wordList = new WordList();
        wordList.add("One");
        wordList.add("Two");
        wordList.add("Three");
        wordList.add("Four");
        wordList.add("Two");
        wordList.add("Five");
        wordList.add("Six");
        wordList.add("Six");
        wordList.add("Two");
        wordList.add("Seven");
        wordList.add("Seven");
        wordList.add("Two");
        wordList.add("Two");
        wordList.add("Seven");
        System.out.println("There are " 
            + String.valueOf(wordList.getOccurances("Two")) 
            + " occurances of Two in this list.");
        System.out.println("There are " 
            + String.valueOf(wordList.getOccurances("Seven")) 
            + " occurances of Seven in this list.");
        System.out.println("There are " 
            + String.valueOf(wordList.getOccurances("One")) 
            + " occurances of One in this list.");
        System.out.println("There are " 
            + String.valueOf(wordList.getOccurances("Ten")) 
            + " occurances of Ten in this list.");
    }
}

Flat View: This topic has 2 replies on 1 page
Topic: EJb questions Previous Topic   Next Topic Topic: voice chat

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use