簡體   English   中英

Java中二維數組的總和

[英]Sum of 2d array in java

我創建了一個名為2d的對象數組,該對象存儲一個String國家和一個double數組。 我在執行某個功能以在某個時期內檢索一個國家的蜂窩統計數據之和時遇到麻煩。 基本上,如果year小於1983(因為數據的起始年份始於1983),它應該返回-1,因為它超出范圍。 嘗試實現的功能稱為

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 
   }

首先,添加數組中包含的國家/地區的數量:

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”,在測試中使用“ 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM