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];
答案 0 :(得分: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
答案 1 :(得分:1)
只是做:
double factor = cpi[k] / cpi[j];
其中k
是frmDate
的选择索引,j
是toDate
的选择索引。
因为现在,您正在尝试使用Strings作为数组的索引。我假设你想为cpi
数组使用相同的索引。
要计算k
和j
,请创建一个函数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
}