3
protected double[] cpi = { 10, 10.1, 10.3, 11.6, 13.7, 16.5 }
  protected CharSequence[] fromDate = { 
          "1914",
          "1915",
          "1916",
          "1917",
          "1918",
          "1919"};
  protected CharSequence[] toDate = { 
          "1914",
          "1915",
          "1916",
          "1917",
          "1918",
          "1919"};

我正在尝试以下操作:

double factor = cpi[frmDate[k]] / cpi [toDate[k]];

我得到以下两个错误:

类型不匹配:无法从 CharSequence 转换为 int

类型不匹配:无法从 CharSequence 转换为 int

我想要做的是......如果选择fromDate是 index = 2 并且toDate是 index = 3 然后计算以下内容:

double factor = cpi[10.3] / cpi[11.6];
4

2 回答 2

3

你可能想要这个:

protected double[] cpi = { 10, 10.1, 10.3, 11.6, 13.7, 16.5 }
  protected CharSequence[] fromDate = { 
          "1914",
          "1915",
          "1916",
          "1917",
          "1918",
          "1919"};
  protected CharSequence[] toDate = { 
          "1914",
          "1915",
          "1916",
          "1917",
          "1918",
          "1919"};
String year1 = "1915";
String year2 = "1918";
indexYear1 = Arrays.asList(fromDate).indexOf(year1); //find the position (index) of year1 => 1
indexYear2 = Arrays.asList(toDate).indexOf(year2); //find the position (index) of year2 => 4
double factor = cpi[indexYear1] / cpi[indexYear2]; // => 10.1 / 13.7
于 2013-10-10T18:29:03.290 回答
1

做就是了:

double factor = cpi[k] / cpi[j];

k的选择索引在哪里, 的选择索引在frmDate哪里。jtoDate

因为现在,您正在尝试使用字符串作为数组的索引。我假设您想对cpi数组使用相同的索引。

为了计算kj,创建一个函数getIndex(CharSequence[] array, CharSequence item)

这是一些伪代码:

private int getIndex(CharSequence[] array, CharSequence item) {
    for(int a = 0; a < array.length; a++) {
        if array[a] is item
            return a;
    }
    return -1; //not in it
}
于 2013-10-10T18:21:54.347 回答