0
import java.util.*;
import java.lang.Iterable;

public class WordGroup {

    String words;

    public static void main(String[] args) {

        WordGroup plato = new WordGroup(
                "You can discover more about a person in an hour of play than in a year of conversation");
        WordGroup roosevelt = new WordGroup(
                "When you play play hard when you work dont play at all");
        String[] platoArray = plato.getWordArray();
        String[] rooseveltArray = roosevelt.getWordArray();
        plato.printArray(platoArray);
        roosevelt.printArray(rooseveltArray);
        System.out.println("____________________________________________________");
        HashSet<String> rooseveltWordSet = roosevelt.getWordSet(platoArray);
        roosevelt.printArray(rooseveltWordSet);

    }

    public WordGroup(String word) {
        words = word.toLowerCase();
    }

    protected String[] getWordArray() {
        String[] wordArray = words.split(" ");
        return wordArray;
    }

    protected HashSet<String> getWordSet(String[] groupedWords)

    {
        HashSet<String> wordSet = new HashSet<String>();
        for (String string : groupedWords) {
                wordSet.add(string);
        }

        String[] wordArray = getWordArray();
        for (String string : wordArray) {
                wordSet.add(string);

        }
        return wordSet;
    }

    protected void printArray(String[] array) {
        for (String string : array) {
            System.out.println(string);
        }
    }
    protected void printArray(HashSet<String> hashWords)
    {
        for(String hashset: hashWords)
        {
            System.out.println(hashset);
        }
    }
    protected void printArray(HashMap<String, Integer> printHashMap)
    {
        for(String string: printHashMap)
        {
            System.out.println(string);
        }
    }
    protected HashMap<String, Integer> getWordCounts()
    {

        String[] wordArray = getWordArray();

        HashMap<String, Integer> wordCounts = new HashMap<String, Integer>();
        for(String string: wordArray)
        {
            int one = 1;
            wordCounts.put(string, +one);

        }

        return null;

    }
}

我试图让 getWordCounts() 制作一个哈希图,其中包含两个字符串中使用的单词并记住该单词和该单词的使用次数。printArray(HashMap printHashMap) 用于迭代在 getWordCounts() 中创建的 HashMap 并打印出数据。

4

2 回答 2

2

getWordCounts将您的方法更改为:

protected Map<String, Integer> getWordCounts()
{
String[] wordArray = getWordArray();
Map<String, Integer> wordCounts = new HashMap<String, Integer>();
List<String> list = Arrays.asList(wordArray);
Set<String> set = new HashSet<String>(list);
for (String str : set) {
    wordCounts.put(str, Collections.frequency(list, str));
}
return wordCounts;
}

并打印地图的元素,将printArray方法更改为:

protected void printArray(HashMap<String, Integer> printHashMap){
Iterator<Entry<String, Integer>> iter = printHashMap.entrySet().iterator();
    while (iter.hasNext()) {
        Entry<String, Integer> next = iter.next();
        Integer value = next.getValue();
        System.out.println("key = " + next.getKey());
        System.out.println("value = " + value);
    }
}
于 2013-11-13T02:47:55.903 回答
1

我已将您的代码更改为可以工作:

import java.util.*;
import java.lang.Iterable;

public class Main {

    String words;

    public static void main(String[] args) {

        Main plato = new Main(
                "You can discover more about a person in an hour of play than in a year of conversation");
        Main roosevelt = new Main(
                "When you play play hard when you work dont play at all");
        String[] platoArray = plato.getWordArray();
        String[] rooseveltArray = roosevelt.getWordArray();
        plato.printArray(platoArray);
        roosevelt.printArray(rooseveltArray);
        System.out.println("____________________________________________________");
        HashSet<String> rooseveltWordSet = roosevelt.getWordSet(platoArray);
        roosevelt.printArray(rooseveltWordSet);
        System.out.println("____________________________________________________");
        roosevelt.printArray(roosevelt.getWordCounts());
        System.out.println("____________________________________________________");

    }

    public Main(String word) {
        words = word.toLowerCase();
    }

    protected String[] getWordArray() {
        String[] wordArray = words.split(" ");
        return wordArray;
    }

    protected HashSet<String> getWordSet(String[] groupedWords)

    {
        HashSet<String> wordSet = new HashSet<String>();
        for (String string : groupedWords) {
                wordSet.add(string);
        }

        String[] wordArray = getWordArray();
        for (String string : wordArray) {
                wordSet.add(string);

        }
        return wordSet;
    }

    protected void printArray(String[] array) {
        for (String string : array) {
            System.out.println(string);
        }
    }
    protected void printArray(HashSet<String> hashWords)
    {
        for(String hashset: hashWords)
        {
            System.out.println(hashset);
        }
    }
    /*Changed*/
    protected void printArray(HashMap<String, Integer> printHashMap)
    {
        for(String string: printHashMap.keySet())
        {
            System.out.println(string + ":" + printHashMap.get(string) );
        }
    }

    /*changed*/
    protected HashMap<String, Integer> getWordCounts()
    {

        String[] wordArray = getWordArray();

        HashMap<String, Integer> wordCounts = new HashMap<String, Integer>();
        for(String string: wordArray)
        {
            if(wordCounts.containsKey(string)){
                wordCounts.put(string, wordCounts.get(string) + 1);
            }else{
                wordCounts.put(string, 1);
            }

        }

        return wordCounts;

    }
}
于 2013-11-13T02:51:00.483 回答