我创建了一个名为2d的对象数组,它存储了一个String国家和一个double数组。我在实施某个功能以检索某个国家/地区的手机统计信息总数时遇到了问题。基本上如果年份小于1983年(自1983年数据开始的起始年份),它应该返回-1,因为它超出界限。尝试实现的函数称为
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 :(得分: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()
方法。您正在使用&#34; USA&#34;在数组和&#34;美国&#34;在您的测试中,您需要忽略大小写比较: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;
}