如题,词典使用arraylist数组 求问如何判断?
public boolean isInDictionary(String temp){
InsertString a=new InsertString();
if(a.x.contains(temp)){
return true;
}
return false;
}
x是我的字典数组 但是不可行 求问其他解决方法?
全部代码在这
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
public class InsertString {
public static ArrayList x = new ArrayList();
public static String[] s =null;
public static void main(String[] args) {
try {
//String encoding="GBK";
File file = new File("F:\\dictionary.txt");
BufferedReader input = new BufferedReader(new FileReader(file));
String ss = new String();
int i = 0;
while ((ss = input.readLine()) != null) {
String[] s = ss.split(",,");
x.add(s[i]);
i = i + 1;
}
//System.out.println(x);
//System.out.println(x);
input.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println("read fail");
}
String input = "你好"; // 要匹配的字符串
new Split(input).start();
}
}
class Split {
private String input=null;
public Split (String input) {
this.input = input;
}
public void start(){
String temp=null;
for(int i=0;i<this.input.length();i++){
temp=this.input.substring(i);//每次从字符串的首位截取一个字,并保存在temp
if(this.isInDictionary(temp)){
System.out.print(temp);
this.input=this.input.replace(temp,"");
}
}
if(null != this.input && !"".equals(this.input)) {
this.input = this.input.substring(0,this.input.length()-1);
this.start();
}
}
//判断当前词是否在字典中
public boolean isInDictionary(String temp){
InsertString a=new InsertString();
if(a.x.contains(temp)){
return true;
}
return false;
}
}
在stackoverflow上有回答说要重写equals和hashcode方法 不是很懂怎么写。。。
debug到判断这一步会自动返回false