Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 只能使用getter方法一次_Java - Fatal编程技术网

Java 只能使用getter方法一次

Java 只能使用getter方法一次,java,Java,我正在从事一个Java项目,在该项目中,我在TextAnalyzer类中使用了下面的getter方法: public Hashtable<String, Double> getTotalFeatureOccurances() { return(feat_occur_total); }//getTotalFeatureOccurances 以下是整个课程: public class TextAnalyzer { TextAnalyzer() { th

我正在从事一个Java项目,在该项目中,我在TextAnalyzer类中使用了下面的getter方法:

public Hashtable<String, Double> getTotalFeatureOccurances() {
    return(feat_occur_total);
}//getTotalFeatureOccurances
以下是整个课程:

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
公共类文本分析器{
TextAnalyzer(){
this.feat_occurrent_total=新哈希表();
}
公共字符串[][]WordOccuranceCount(字符串文本、向量特征){
字符串[][]出现=新字符串[features.size()][features.size()];
对于(int ndx=0;ndx

清除哈希表。由于返回了对原始变量的引用,因此清除了哈希表本身,而不是副本。因此,再次返回它将返回已清除的哈希表。

此:


清除哈希表。由于您返回了对原始变量的引用,因此您清除了哈希表本身,而不是副本。因此,再次返回它将返回已清除的哈希表。

您的getter将返回对私有
feat\u occurrent\u total
字段的引用,而不是副本。在此之后,
TextAnalyzer.feat\u occurrent\u total引用和getter返回的引用引用同一实例
使用从getter返回的引用,该引用将清除顶部的代码段和
TextAnalyzer
实例引用的映射。

getter将返回对私有
feat\u occure\u total
字段的引用,而不是副本。
TextAnalyzer.feat\u occure\u total
引用d getter返回的引用引用同一实例
使用从getter返回的引用,该引用清除顶部的代码段和
TextAnalyzer
实例引用的映射。

顺便说一句,Hashtable已过时-在单线程代码中首选HashMap,或者在其他情况下选择ConcurrentHashMap。这可能只是迂腐,但如果创建Hashtable和ne在你的类中更改私有引用时,它也应该是“最终的”。这向类的用户表明了您的意图,并且最终变量允许编译器执行它可能无法执行的优化。顺便说一句,Hashtable已过时-在单线程代码中更喜欢HashMap,或者在其他情况下更喜欢ConcurrentHashMap。这可能只是迂腐,但如果您创建了Hashtable而从不执行更改类中的私有引用,它也应该是“final”。这向类的用户表明了您的意图,final变量允许编译器执行它可能无法执行的优化。它是一个具有少量
t
:)的
Hashtable
)巨大的帮助。我没有意识到它返回了对哈希表的引用,很高兴知道。谢谢你的帮助。它是
Hashtable
,有点
t
:)巨大的帮助。我没有意识到它返回了对哈希表的引用,很高兴知道。谢谢你的帮助。
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
feat_occur_total.clear();