如果从其自己的类中调用,我的代码返回预期的entrySet()。但是,如果我通过Main中的getter方法调用它,它将返回一个空表格。为什么?
class Results
{
Hashtable<String, Double> langScores ;
public Results()
{
langScores = new Hashtable<String, Double>() ;
}
public void addScores(double pL1, double pL2, double pL3)
{
langScores.put("English", pL1 ) ;
langScores.put("French", pL2 ) ;
langScores.put("German", pL3 ) ;
System.out.println(langScores.entrySet()) ;
}
public Set<Map.Entry<String, Double>> getWinner()
{
return langScores.entrySet() ;
}
}
主要内容:
class LanguageIdentifier
{
public static void main(String[] args)
{
Results winner = new Results() ;
AnalyseText analyse = new AnalyseText() ; //addScores called from this class
analyse.analyseText() ;
System.out.println(winner.getWinner()) ;
}
}
输出
[German=0.0040, French=0.0030, English=0.02] // print statement within Results class works
[] // print statement within Main class prints empty table !?