example illustrates autoboxing and unboxing

 Design Patterns
Most developers claim to experience an epiphany reading this book. If you've never read the Design Patterns book then you have suffered a very serious gap in your programming education that should be remedied immediately.
 
The following example illustrates autoboxing and unboxing, along with generics and the for-each loop.
 
 
package test;
 
import java.util.*;
 
//Prints a frequency table of the words on the command line
public class Frequency {
 public static void main(String[] args) {
      Map<String, Integer> m = new TreeMap<String, Integer>();
      for (String word : args) {
          Integer freq = m.get(word);
          m.put(word, (freq == null ? 1 : freq + 1));
      }
      System.out.println(m);
 }
}
//這是計算輸入的字符串,每一個詞使用架頻率.
//命令行輸入:if it is to be it is up to me to do the watusi
//該結果應爲:be=1, do=1, if=1, is=2, it=2, me=1, the=1, to=3, up=1, watusi=1
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章