符号表

符号表,有时候被称为字典,类似于那本将单词释义按照字母排列顺序排列起来的历史悠久的参考书!

符号表,最主要的目的是将一个键和一个值进行联系,实现的重点是其使用的数据结构,put()和get()方法

接下来是一个测试用例,FrequendCounter用例会从标准的输入中得到一列字符串,并且记录每个(长度至少达到指定的阈值)字符串的出现次数,然后遍历所有键,找出出现频率最高的键

 1 public class FrequentCounter
 2 {
 3    public static void  main (String []args)
 4    {
 5      int minlen=Integer.parseInt(args[]);
 6      ST<String,Integer> st=new ST<String,Integer>;
 7     while(!StdIn.isEmpty())
 8    {
 9      String word=StdIn.readString();
10     if(word.length()<minlen)  continue //忽略较短的单词 
11     if (!st.contains(word()) st.put(word,1);
12     else  st.put(word,st,get(word)+1);
13     }
14     //找出出现频率最高的单词
15     String max=“”;
16     st.put(max,0)
17     for(String word : st.keys())//遍历
18     if(st.get(word)>st.get(max))
19     max=word;
20    stdOut.println(max+"  "+st.get(max));
21   }
22 }

 

posted on 2016-05-16 21:21  Cultivate  阅读(345)  评论(0)    收藏  举报

导航