?? dictionary.java
字號:
package entity;
//字典類
public class Dictionary {
private String name; // 字典名
private int[] length; // 字典長度 按首字符分組 0-25為字母'a|A'-'z|Z' 26為非字母 27為總長度
private String[][] words; // 單詞表 按首字符分組 0-25為字母'a|A'-'z|Z' 26為非字母
// 構造函數
// words 字典單詞表
// length 單詞表長度
public Dictionary(String[] words, int number) {
name = words[0]; // 第0個單詞為字典名
length = countWordNnumber(words, number); // 計算并保存字典長度
this.words = saveWords(words, length);
}
// 獲取字典名
public String getName() {
return name;
}
// 獲取字典長度
public int[] getLength() {
return length;
}
// 按首字符獲取字典長度
// c 首字符 非字母返回所有非字母開頭單詞數目
public int getLength(char c) {
int temp = charTOint(c);
return length[temp];
}
// 獲取字典總長度
public int getAllLength() {
return length[27];
}
// 獲取所有單詞 按首字符分組0-25為字母'a|A'-'z|Z' 26為非字母
public String[][] getWords() {
return words;
}
// 按首字符獲取單詞
// c 首字符 非字母返回所有非字母開頭單詞
public String[] getWords(char c) {
int temp = charTOint(c);
return words[temp];
}
// 把char轉換為int 'a|A'到'z|Z'轉為0-25 其他字符返回26
// c 要轉換的字符
public int charTOint(char c) {
int temp = (int) c; // 把char轉為ASCII碼
if (temp < 97) { // 少于'a'的字符加32 以便與小寫字母對比
temp = temp + 32;
}
temp = temp - 97; // 把'a'到'z'轉為0-25
if (temp < 0 || temp > 25) {
return 26;
}
return temp;
}
// 計算以不同字母開頭的單詞的數目
// words 單詞表 number 單詞總數
private int[] countWordNnumber(String[] words, int number) {
int[] length = new int[28];
int i = 0;
for (i = 1; i < number; i++) { // 從第一個單詞開始掃描
length[charTOint(words[i].charAt(0))]++; // 相應單詞數加1
}
length[26] = number;
for (i = 0; i < 26; i++) { // 計算非字母開頭的單詞數
length[26] = length[26] - length[i];
}
length[26]--; // 減去字典名
length[27] = number - 1; // 保存總單詞數
return length;
}
// 返回按首字母分組的單詞
// words 要分組的單詞 length 以各字母開頭的單詞的數目
private String[][] saveWords(String[] words, int[] length) {
String w[][] = new String[27][];
int i = 0, j = 0;
int[] temp = new int[27];
for (i = 0; i < 27; i++) { // 生成以各字母開頭的單詞數組
w[i] = new String[length[i]];
}
for (i = 1; i <= length[27]; i++) { // 從第一個單詞開始掃描
j = charTOint(words[i].charAt(0)); // 記錄首字母
w[j][temp[j]] = words[i].substring(0, words[i].length() - 1); // 單詞添加到相應分組
temp[j]++; // 分組位置加1
}
return w;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -