变量输出[]可能尚未初始化

时间:2016-01-06 18:55:15

标签: java arrays

我正在尝试用Java编写一些东西,因为谷歌表格对我来说过于复杂。 (如果你想知道统计数据是什么,我有一个与中国人口有关的学校项目。)变量output[]显然没有被初始化。就我而言,一切都应该有效(该计划旨在输出年间人口的差异)。

import java.util.*;
import java.lang.*;
import java.io.*;

class ActualStatistics{

    public static void main(String[] args){
        double[] year = {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,1989,1990,1991,1992
        ,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013};
    double[] population = {667300000,667300000,660300000,665800000,682300000,698400000,715200000,735400000,754600000,774500000,796000000,818300000,841100000,862000000,881900000,900400000,916400000,930700000,943500000,956200000,969000000,981200000,993900000,1009000000,1023000000,1037000000,1051000000,1067000000,1084000000,1102000000,1119000000,1135000000,1151000000,1178000000,1192000000,1205000000,1218000000,1230000000,1242000000,1253000000,1263000000,1272000000,1280000000,1288000000,1296000000,1304000000,1311000000,1318000000,1325000000,1331000000,1338000000,1344000000,1351000000,1357000000};
        System.out.println("Year   |   Population");
        for(int counter=1;counter<=population.length;counter++){
            double[] output;
            output[counter] = population[counter+1] - population[counter];
            System.out.println(year[counter]+"   |   "+output[counter]);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您收到错误是因为您没有初始化数组:

double[] output = new double[SIZE];

请注意,在这种情况下您实际上不需要数组:

for(int counter=1;counter<=population.length;counter++){
    double output;
    output= population[counter+1] - population[counter];
    System.out.println(year[counter]+"   |   "+output);
}