2014-10-03 4 views
0

String 국가와 double 배열을 저장하는 2 차원 객체 배열을 만들었습니다. 특정 기간 동안 국가의 셀룰러 통계 합계를 검색하는 기능을 구현하는 데 문제가 있습니다. 기본적으로 연도가 1983 년보다 작 으면 (데이터가 시작된 1983 년 이래로) 연도가 -1에서 벗어나면 -1을 반환해야합니다. 구현하려는 기능은 Java에서 2 차원 배열의 합

getNumSubscriptionsInCountryForPeriod(String country, int startYear, int endYear) 

그것은 당신이 통계는 올해의 시작과 연말을 인쇄하려는 국가를 취

라고합니다. 그런 다음 올해의 지수 차이를 가져와 선택한 기간의 합계를 추가하지만 구현하는 데 문제가 있습니다. 예를 들어 내가 통과하면 ("USA", 1983, 1989) 1983 년과 1989 년 사이에 총 통계의 합계를 인쇄해야합니다. 표시는 usa (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 방법 당신은 비교해야 자바 방식으로 문자열. == opeator가 아니라 equals() 방법. 당신은 배열에 "USA"를 사용하여 테스트에 "미국", 그래서 당신은 비교 무시의 경우 필요가있다 : equalsIgnoreCase() :

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; 
} 
:

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

다음은 당신이 당신 방법을 수정할 수 있습니다