2014-10-03 84 views
0

我創建了一個叫做double的對象數組,它存儲了一個String國家和一個雙數組。我在執行一項功能時遇到了麻煩,無法在一段時間內檢索一個國家/地區的細胞統計數據總和。基本上,如果年份少於1983年(自1983年開始數據開始以來),它應該返回-1導致它超出界限。試圖實施的功能被稱爲java中2d數組的總和

getNumSubscriptionsInCountryForPeriod(String country, int startYear, int endYear) 

它需要您打算打印統計信息的國家,年初和年底。然後通過獲取一年的指數差額來添加所選時間段的總和,但我在實施時遇到了問題。例如,如果我通過(「美國」,1983年,1989年),它應該列出1983年至1989年間總數的總和。顯示應該是:美國(1983年至1989年):3.14。嘗試訪問對象中的值時無法轉換/轉換爲對象時遇到了麻煩。請建議將有所幫助。

public class CellularData { 

private Object [][]array; 

public CellularData(int rows, int columns, int year) 
{ 
    array = new Object[rows+1][columns+1]; 
    array[0][0] = "Country"; 
    this.year = year; 
    for(int i=1;i<=columns;i++) 
    { 
    array[0][i] = year++; 
    } 
} 

public void addCountry(String country, double []num) 
{ 
    for(int i=0;i<array.length;i++) 
    {  
    if(array[i][0] == null) 
    { 
     addCountry(country, num, i); 
     break; 
    } 
    } 
} 
private void addCountry(String country, double []num, int row) 
{ 
    array[row][0] = country; 
    for(int j = 1;j<array[row].length;j++) 
    { 
     array[row][j] = num[j-1]; 
    } 
} 
public double getNumSubscriptionsInCountryForPeriod(String country, int sYear, int eYear) 
{//trouble implementing the function 
    double sum = 0; 
    int indexStart = (int)(sYear - ((Integer) array[0][1]).doubleValue()); 

    for(int i=1;i<array.length;i++) 
    { 
     if(array[i][0]==country && ((sYear<year)||(eYear>year))) 
     { 
      //System.out.print(array[i][0]); 

     } 
    } 
    return sum; 
} 

public String toString() 
{ 
    for(Object []a: array) 
    { 
     for(Object k:a) 
     { 
      System.out.print(k + "\t"); 
     } 
     System.out.println(); 
    } 
    return " "; 
} 
} 

public class TestCellularData { 

public static void main(String []args) 
{ 
    final double[] usaPartial = {0,0,0.14,.28,.5,.83,1.39}; 
    final double[] canadaPartial = {0,0,.05,.23,.37,.75,1.26}; 
    final double[] mexicoPartial = {0,0,0,0,0,0,0.01}; 

    int numRows = 3; 
    int numColumns = canadaPartial.length; 
    int startingYear = 1983; 

    CellularData datatable = new CellularData(numRows, numColumns, startingYear); 
    datatable.addCountry("USA", usaPartial); 
    datatable.addCountry("Mexico", mexicoPartial); 
    datatable.addCountry("Canada", canadaPartial); 

    System.out.println(datatable); 

    System.out.printf("usa (1983 to 1989): %.2f \n",  datatable.getNumSubscriptionsInCountryForPeriod("usa",1983,1989)); 
    // country is "usa", subscriptions from 1983 to 1989 
    // the output is: 
    // usa (1983 to 1989): 3.14 
    System.out.printf("mexico (1983 to 1989): %.2f \n", datatable.getNumSubscriptionsInCountryForPeriod("mexico",1983,1989)); 
    // country is "mexico", subscriptions from 1983 to 1986 
    // the output is: 
    // mexico (1983 to 1989): 0.01     
    // NOTE: in order to get this result, you must test beyond the sample data included here and refer to the CSV file. 
    System.out.printf("canada (1890 to 2000): %.2f \n", datatable.getNumSubscriptionsInCountryForPeriod("canada",1890, 2000)); 
    // the output is: 
    // ERROR : requested year 1890 is less than starting year 1893 
    // canada (1890 to 2000): -1.00 
    } 

回答

0

首先,添加數量的國家,你有數組:

public class CellularData { 

    private Object[][] array; 
    private final int year; 
    private int countryNum = 0; // number of countries in array 

    public CellularData(int rows, int columns, int year) { 
     array = new Object[rows + 1][columns + 1]; 
     array[0][0] = "Country"; 
     this.year = year; 
     for (int i = 1; i <= columns; i++) { 
      array[0][i] = year++; 
     } 
     countryNum = 1; 
    } 

現在你可以改變你的addCountry方法:

public void addCountry(String country, double[] num) { 
    addCountry(country, num, countryNum++); 
} 

接下來,在getNumSubscriptionsInCountryForPeriod方法,你要比較以java方式的字符串。不是通過==操作員,而是通過equals()方法。您在使用數組「USA」和「美國」,在您的測試,所以你需要忽略大小寫比較:equalsIgnoreCase()

if (country.equalsIgnoreCase((String)array[i][0])) { 

接下來,您可以修改你的方法:

public double getNumSubscriptionsInCountryForPeriod(String country, int sYear, int eYear) { 
    double sum = 0; 

    for (int i = 1; i < array.length; i++) { 

     if (country.equalsIgnoreCase((String) array[i][0])) { 
      int start = 1 + sYear - year; 
      int end = start + (eYear - sYear); 

      if (start >= 0 && end < array[i].length) { 

       for (int k = start; k <= end; k++) { 
        // System.out.println(">> " + country + " adding " + array[i][k]); 
        sum += (Double) array[i][k]; 
       } 
      } 
     } 
    } 

    return sum; 
}