java-如何获取数组的索引

java-如何获取数组的索引,第1张

概述protecteddouble[]cpi={10,10.1,10.3,11.6,13.7,16.5}protectedCharSequence[]fromDate={"1914","1915","1916","1917","1918",&q

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]];

我都收到以下错误:

Type mismatch: cannot convert from CharSequence to int

Type mismatch: cannot convert from CharSequence to int

我想做的是…如果fromDate的选择是index = 2而toDate的选择是index = 3,则计算以下内容:

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

解决方法:

您可能想要这样:

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 => 1indexYear2 = Arrays.asList(toDate).indexOf(year2); //find the position (index) of year2 => 4double factor = cpi[indexYear1] / cpi[indexYear2]; // => 10.1 / 13.7
总结

以上是内存溢出为你收集整理的java-如何获取数组索引全部内容,希望文章能够帮你解决java-如何获取数组的索引所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1087451.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存