java只能使用getter方法一次
我正在从事一个Java项目,在该项目中,我在TextAnalyzer类中使用了下面的getter方法:
public Hashtable<String, Double> getTotalFeatureOccurances() {
return(feat_occur_total);
}//getTotalFeatureOccurances
我还有private class变量:
private Hashtable<String, Double> feat_occur_total;
我使用getter,向散列添加更多术语,然后想再次获取散列,但它总是返回空。更糟糕的是,如果我不从哈希中添加或删除任何内容,但执行两次get,我仍然会收到并在第二次清空哈希
以下是我的主要代码:
TextAnalyzer ta = new TextAnalyzer();
feat_occur_cat = ta.wordOccurancesCount(text, features);
feat_occur_total = ta.getTotalFeatureOccurances();
Enumeration<Double> e = feat_occur_total.elements();
while(e.hasMoreElements()) {
System.out.println(e.nextElement());
}//while
feat_occur_total.clear();
feat_occur_total = ta.getTotalFeatureOccurances();
e = feat_occur_total.elements();
System.out.println("\n\nSECOND GET\n\n");
while(e.hasMoreElements()) {
System.out.println(e.nextElement());
}//while
我得到了输出:
2.0
1.0
5.0
1.0
1.0
3.0
2.0
3.0
SECOND GET
下面是全班同学:
public class TextAnalyzer {
TextAnalyzer() {
this.feat_occur_total = new Hashtable<String, Double>();
}
public String[][] wordOccurancesCount(String text, Vector<String> features) {
String[][] occur = new String[features.size()][features.size()];
for(int ndx=0; ndx<features.size(); ndx++) {
int count=0;
Pattern p = Pattern.compile("(?:^|\\s+|\\()\\s*(" + features.elementAt(ndx).trim() + ")\\w*(?:,|\\.|\\)|\\s|$)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.CANON_EQ);
Matcher m = p.matcher(text);
m.usePattern(p);
while(m.find())
count++;
occur[ndx][0] = features.elementAt(ndx);
occur[ndx][1] = String.valueOf(count);
if(this.feat_occur_total.containsKey(features.elementAt(ndx))) {
double temp = this.feat_occur_total.get(features.elementAt(ndx));
temp += count;
this.feat_occur_total.put(features.elementAt(ndx), temp);
}//if
else {
this.feat_occur_total.put(features.elementAt(ndx), Double.valueOf(count));
}//else
}//for
return(occur);
}//word
public Hashtable<String, Double> getTotalFeatureOccurances() {
return(feat_occur_total);
}//getTotalFeatureOccurances
private Hashtable<String, Double> feat_occur_total;
}//TextAnalyzer
共 (0) 个答案