开发者

Only able to use getter method once

开发者 https://www.devze.com 2023-01-16 12:47 出处:网络
I\'m working on a Java project in which I have the getter method below in the class TextAnalyzer: public Hashtable<String, Double> getTotalFeatureOccurances() {

I'm working on a Java project in which I have the getter method below in the class TextAnalyzer:

public Hashtable<String, Double> getTotalFeatureOccurances() {
    return(feat_occur_total);
}//getTotalFeatureOccurances

I also have the private class variable:

private Hashtable<String, Double> feat_occur_total;

I use the getter, add more terms to the hash, and then want to get the hash again, however it always comes back empty. Even worse if I don't add or remove anything from the hash, but do two gets, I still receive and empty hash the second time.

Here is my code in main:

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

I get the output:

2.0
1.0
5.0
1.0
1.0
3.0
2.0
3.0


SECOND GET

Here is the entire class:

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));
     开发者_Python百科       }//else
        }//for

        return(occur);
    }//word

    public Hashtable<String, Double> getTotalFeatureOccurances() {
        return(feat_occur_total);
    }//getTotalFeatureOccurances

    private Hashtable<String, Double> feat_occur_total;

}//TextAnalyzer


This:

feat_occur_total.clear();

Clears the Hashtable. Since you returned a reference to the original variable, you cleared the Hashtable itself, not a copy. So returning it again will return the cleared Hashtable.


Your getter is returning a reference to the private feat_occur_total field, not a copy. After this, both the TextAnalyzer.feat_occur_total reference and the reference returned by the getter refer to the same instance. You are calling clear() using the reference returned from the getter, which clears the map that both the code snippet at the top and the TextAnalyzer instance refer to.

0

精彩评论

暂无评论...
验证码 换一张
取 消