我的问题是当我运行我的主要方法时,什么都没有打印。我对HashSet
s 很陌生,我担心它因为一些非常愚蠢的事情而不起作用。
dic
最初是 an ArrayList
,我只是想将其转换为HashSet
s 以提高效率。
private Set<String>dic = new HashSet<String>(100000);
public void dictionary(){//reads/intializes arraylist dic from a file
File data = new File("dictionaryForJava.txt");
Scanner scanner=null;
try {
scanner = new Scanner(data);
while (scanner.hasNextLine()) {
dic.add(scanner.nextLine());
}
} catch (FileNotFoundException fnfe) {}
if(scanner!=null)scanner.close(); // need if if file missing
}
public void print () throws IOException{
Iterator it = dic.iterator();
while(it.hasNext())
{
String value =(String)it.next();
System.out.println(value);
}
}
public static void main (String[] args)throws IOException{
words test = new words();
test.dictionary();
test.print();
}