从引用数组输出错误的总和

时间:2014-10-17 08:45:39

标签: java

大家好我从csv文件中读取一个字符串,多年和两倍。基本上读取国家名称,年份和细胞数据的统计数据。例如:

Country Name    1960    1961    1962    1963    1964    1965    1966    1967    1968    1969    1970    1971    1972    1973    1974    1975    1976    1977    1978    1979    1980    1981    1982    1983    1984    1985    1986    1987    1988    1989    1990    1991    1992    1993    1994    1995    1996    1997    1998    1999    2000    2001    2002    2003    2004  
 Aruba           0         0      0       0        0      0       0      0       0   0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.029310471 0   0   2.138784453 3.605985937 3.98141538  6.16435217  13.48254011 16.50927821 57.05427692 65.05605558 72.10431377 99.64250268.

我的程序能够读取数据,但输出错误的总和到屏幕上。我创建了2个类。一个名为subscriptionYear,它有两个名为year的变量(存储订阅数据的年份)和订阅(存储特定年份的订阅数)。被称为国家的第二类存储来自每个国家的国家和订阅数据。我的方法getNumSubscriptions计算错误,因为它只读取2012年的内容,而不是总和。如果我输入其他年份它将总和读为0并且仅读取2012年的最后一年。我如何使用年份的指数位置计算总和来计算1960年到2012年之间的总和。请有人告诉我我做错了什么。

public class SubscriptionYear {

private int year;
private double subscriptions;

public SubscriptionYear(int year,double subscriptions)
{
    this.year = year;
    this.subscriptions = subscriptions;
    setYear(year);
    setSubscription(subscriptions);
}
public void setYear(int Year)
{
    this.year= Year;
}
public void setSubscription(double value)
{
    this.subscriptions = value;
}
public int getYear()
{
    return year;
}
public double getSubscription()
{
    return subscriptions;
}
public String toString()//returns number of subscriptions
{
    return "Number of Subscriptions: "+subscriptions;
}
}

班级国家:

public class Country {

private String countryNames;
private SubscriptionYear[] subscriptions;
private int size;

public Country(String country, int arraylength)
{
    this.countryNames = country;
    this.size = arraylength;
    subscriptions = new SubscriptionYear[size];
}
public void addSubscriptionYear(int year, double subscription)
{
    for(int i=0;i<subscriptions.length;i++)
    {
        subscriptions[i] = new SubscriptionYear(year, subscription);
    }
        System.out.print(subscriptions[0].getYear()+"\t");

}
public double getNumSubscriptionsForPeriod(int start, int end)
{
    double sum =0;
    int head = subscriptions[0].getYear()-start;
    int tail = end-start;
    for(int k=head;k<=tail;k++)
    {
        sum += subscriptions[k].getSubscription();
    }
    return sum; 
}
  }

测试文件:

Country [] countries;
    //countries = new Country[NUM_COUNTRIES_TO_TEST];   // Note: Use this for initial testing of your implementation.
    countries = new Country[countryNames.length]; //READS 253 COUNTRIES              

    Country current;

    for (int countryIndex = 0; countryIndex < countries.length; countryIndex++)
    {
        int numberOfYears = yearLabels.length;   // READS THE YEAR BTWN 1960 AND 2012

        current = new Country(countryNames[countryIndex], numberOfYears); //CALLS CONSTRUCTOR

        for (int yearIndex = 0; yearIndex < numberOfYears; yearIndex++)
        {
            double [] allSubscriptions = parsedTable[countryIndex];
            double countryData = allSubscriptions[yearIndex];
            current.addSubscriptionYear(yearLabels[yearIndex], countryData); //STORES THE YEAR AND SUBSCRIPTION DATA OF EACH YEAR
        }
        countries[countryIndex] = current;
    }


    System.out.printf(countryNames[0] + " (1960 to 2012): %.2f \n", countries[0].getNumSubscriptionsForPeriod(1960,2012));
    // the output is: Aruba (1960 to 2012): 1170.50 

应输出1170.50作为总金额,但它只输出131.86,这是2012年阿鲁巴的订阅数据。

2 个答案:

答案 0 :(得分:1)

正如汤姆在评论中指出的那样 - 问题在于addSubscriptionYear,由于某种原因,它将订阅数组中的每一个条目替换为最新添加的条目。同样,引用Tom,解决方案可能是使用与getNumSubscriptionsForPeriod方法中使用的相同的algorythm:

 subscriptions[subscriptions[0].getYear()-year] = new SubscriptionYear(year, subscription);

当然完全放弃了循环。

您也可以删除奇怪的索引并始终使用整个数组。出于某种原因,您在SubscriptionYear中有该年份参数。另外,只需在下一个空白区域中添加SubscriptionYear,然后在整个数组中添加总和,如果此时订阅的订阅数量在边界内,则添加总和。

然而,使用带有年份作为关键字的地图和订阅作为每个国家/地区的价值比使用获得正确索引的神秘算法的数组更容易。比你简单地拥有:

Map<Integer,Double> subscriptions = new HashMap<Integer,Double>();
public void addSubscriptionYear(int year, double subscription)
{
  subscriptions.put(year,subscription);
}
public double getNumSubscriptionsForPeriod(int start, int end)
{
  double sum = 0;
  for(int i=start;i<=end;i++){
    sum += subscriptions.get(year);
  }
  return sum;
}

答案 1 :(得分:0)

问题在于方法:

public void addSubscriptionYear(int year, double subscription)
{
    for(int i=0;i<subscriptions.length;i++)
    {
        subscriptions[i] = new SubscriptionYear(year, subscription);
    }
    System.out.print(subscriptions[0].getYear()+"\t");

}

此方法每次创建具有相同值的订阅数组。

你应该(在leas)使用列表而不是数组并使用.add()方法,或者使用年份作为索引的散列图。

d