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

📄 dictionary.java

📁 实现英文的拼写检查
💻 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 + -