【问题标题】:How to write a method that prints out a histogram?如何编写打印出直方图的方法?
【发布时间】:2014-05-17 00:41:11
【问题描述】:

我必须为实验室编写这个程序。我必须基本上说明什么是hashmap(键和值)以及声明的基本操作、.add()、.get(),以及如何从映射中获取键和值。然后,您将使用 woodchucks.txt 输入文件将其应用于频率直方图问题。我已经完成了所有这些,但我一直坚持如何编写打印直方图的方法。有人可以帮我吗?

import java.io.*;
import java.util.*;

public class Lab8
{
public static void main(String args[]) throws Exception
{
    BufferedReader infile = new BufferedReader(new FileReader(args[0]));
    HashMap<String,Integer> histogram = new HashMap<String,Integer>();
    String word;
    while ( (word = infile.ready()) != null )
    {
        if(histogram.get(word)==null)
            histogram.put(word,1);
        else
            histogram.put(word, histogram.get(word)+1);

    }   
             // YOUR CODE HERE

    infile.close();
    printHistogram( histogram );

} // END MAIN
// YOU FILL IN THIS METHOD
// READ PROBLEM SPECIFICATION TO SEE WHAT IS THE 80% vs 100% CREDIT SOLUTION

private static void printHistogram( HashMap<String,Integer> hm )
{

    // YOU CODE HERE
}
} // END LAB8 CLASS

我会这样打印直方图吗?

for ( int i = 0; i < histogram.length; i++ )
         {
         output += "\n" + i + "\t" + histogram[ i ] + "\t";

         for ( int j = 1; j <= histogram[ i ]; j++ ) 

【问题讨论】:

  • 哦,对不起,没有。我的老师在他希望我们添加代码的地方制作了那些 cmets。如您所见,这就是我必须制作该方法的地方。你能给我一些关于如何处理它的提示......我相信我必须使用 for 循环?
  • 先试试吧……好像是你的课堂作业!
  • 如果您可以在 while 循环中看到,我已经在那里添加了代码并完成了它。这只是我坚持的私有方法
  • 您需要自己尝试一下,如果遇到困难,请回来提问。
  • 好吧,让我试一试...我会编辑问题

标签: java hashmap histogram


【解决方案1】:

因此,如果我要这样做,我将从创建两个类开始,其中一个我可以保存单词和数字的出现,例如我的 HistogramItem

public class HistogramItem implements Comparable<HistogramItem> {
        @Override
        public String toString() {
            return "HistogramItem [word=" + word + ", occurence=" + occurence
                    + "]";
        }

        public int getOccurence() {
            return occurence;
        }

        public void updateOccurence() {
            this.occurence++;
        }

        public String getWord() {
            return word;
        }

        public HistogramItem(String word) {
            super();
            this.word = word;
        }

        private final String word;
        private int occurence = 0;

        @Override
        public int compareTo(HistogramItem o) {

            if (occurence == o.occurence) {
                return word.compareTo(o.word);
            }
            return o.occurence-occurence;
        }

    }

另一个将是我的实际直方图

public class Histogram {
        private Map<String, HistogramItem> map = new HashMap<>();
        private List<HistogramItem> list = new ArrayList<>();

        public void addWord(String word) {
            HistogramItem item = map.get(word);
            if (item == null) {
                item = new HistogramItem(word);
                map.put(word, item);
                list.add(item);
            }
            item.updateOccurence();
        }

        public List<HistogramItem> getList() {
            Collections.sort(list);
            return list;
        }

    }

我使用了两个集合,hashmap,因为搜索条目比在列表中要快得多,但列表更容易排序,列表可以在请求时创建和排序

这如何适合您的原始练习?简单

public static void main(String args[]) throws Exception
{
    BufferedReader infile = new BufferedReader(new FileReader(args[0]));
    Histogram histogram = new Histogram();
    String word;
    while ( (word = infile.ready()) != null )
    {
       histogram.addWord(w);

    }   
             // YOUR CODE HERE

    infile.close();

//   Below line prints histogram, can be placed in printHistogram method
for (HistogramItem item : histogram.getList()) {
            System.out.println(item.toString());
        }

} // END MAIN

【讨论】:

    【解决方案2】:

    我想通了,这就是答案...

    import java.io.*;
    import java.util.*;
    
    public class Lab8
    {
    public static void main(String args[]) throws Exception
    {
        BufferedReader infile = new BufferedReader(new FileReader(args[0]));
        HashMap<String,Integer> histogram = new HashMap<String,Integer>();
        String word;
        while ((infile.ready()))
        {
            word = infile.readLine();
            if(histogram.get(word)== null) //if the word your currently on is not duplicated
            {
                histogram.put(word,1);
            }
            else
            {
                histogram.put(word, histogram.get(word)+1);
            }
        }   
                 // YOUR CODE HERE
    
        infile.close();
        printHistogram( histogram );
    
    } // END MAIN
    // YOU FILL IN THIS METHOD
    // READ PROBLEM SPECIFICATION TO SEE WHAT IS THE 80% vs 100% CREDIT SOLUTION
    
    private static void printHistogram( HashMap<String,Integer> hm )
    {
        List <String> keys = new ArrayList<String> (hm.keySet());   
        Collections.sort(keys);
        for (String key: keys) 
        {
            System.out.println(key + "\t"  + hm.get(key));
    
        }
    } 
    }// END LAB8 CLASS
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 2011-12-20
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 2013-09-26
    相关资源
    最近更新 更多