⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dictionary.java

📁 Java学习源代码检索系统免费版
💻 JAVA
字号:
//==============================================================
// Dictionary.java - Create dictionary using TreeMap and LinkedList
//
// Java学习源代码检索系统 Ver 1.0 20031015 免费正式版
// 版权所有: 中国IT认证实验室(www.ChinaITLab.com)
// 程序制作: ChinaITLab网校教研中心
// 主页地址: www.ChinaITLab.com    中国IT认证实验室
// 论坛地址: bbs.chinaitlab.com  
// 电子邮件: Java@ChinaITLab.com
//==============================================================

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

class Dictionary {

// Construct TreeMap dictionary container
  static TreeMap dict = new TreeMap();

// Lookup and show definition for the specified word (key)
 static void showDefinition(String word) {
  LinkedList defs = (LinkedList)dict.get(word);
  if (defs == null) return;  // Ignore if not there
  ListIterator L = defs.listIterator();
  int count = 1;  // Definition counter
  System.out.println("\n" + word);
  while (L.hasNext()) {
    String definition = (String)L.next();
    System.out.println(count++ + ". " + definition);
  }   
 }

// Display entire dictionary
 static void showDictionary() {
  Iterator I = dict.keySet().iterator();
  while (I.hasNext())
   showDefinition((String)I.next());
 }

// Add a new word and/or definition
 static void addWord(String word, String definition) {
  if (dict.containsKey(word)) {
   LinkedList defs = (LinkedList)dict.get(word);
   defs.add(definition);  // Add new definition only
  } else {
   LinkedList defs = new LinkedList();  // New list
   defs.add(definition);  // Add definition to new list
   dict.put(word, defs);  // Add word and defs association
  }
 }

 public static void main(String args[]) {

// Read words and definitions into the container
  try {
   FileReader fr = new FileReader("Dictionary.txt");
   BufferedReader br = new BufferedReader(fr);
   String word = br.readLine();
   while (word != null) {
    addWord(word, br.readLine());  // Add word and definition
    br.readLine();         // Skip blank line
    word = br.readLine();  // Read next word
   }
  } catch (FileNotFoundException e) {
   System.out.println("File not found: " + e.getMessage());
  } catch (IOException e) {
   System.out.println("I/O error: " + e.getMessage());
  }

// Look up one word or show entire dictionary
 if (args.length == 0)
  showDictionary();         // Show all
 else
  showDefinition(args[0]);  // Show selection

 }  // main
} // class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -