我必须为实验室编写这个程序。我必须基本上说明一个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++ )
答案 0 :(得分:0)
所以如果我要这样做,我会从创建两个类开始,一个可以保存单词和数字出现的类,例如我的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
答案 1 :(得分:0)
我明白了,这就是答案......
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