【问题标题】:How to find array index value in android如何在android中找到数组索引值
【发布时间】:2016-06-25 01:34:14
【问题描述】:

我正在寻找一种方法来查找数组的索引位置,我想将该值存储在 String 中,但是我们该如何做到这一点,请帮帮我

我的数组列表:-

  String[] planets = new String[] { "Mercury","Mercury", "Venus", "Earth", "Mars",
            "Jupiter", "Saturn", "Uranus", "Neptune"};

 String[] Ruppes = new String[] { "100", "200","1000", "300", "400",
            "500", "600", "700", "800"};

【问题讨论】:

标签: android arrays


【解决方案1】:

你可以使用 Arrays.asList 来做

Arrays.asList(planets).indexOf("Mercury") // pass value

Arrays.asList(Rupps).indexOf("100") // pass value

Arrays.asList(planets).indexOf(2); //pass index

【讨论】:

  • 不,这行显示错误 String list = Arrays.asList(planets).indexOf(position);
  • 当我将该值存储在 String 中有错误显示该行
  • 我认为我们必须在这里进行类型转换
【解决方案2】:
private int foundIndex(String query) {
    for (int i = 0; i < planets.length; i++) {
        if (planets[i].equals(query)) {
            return i;
        }
    }
    return -1;
}

...在代码中:

        int index = foundIndex("Venus");
        if(index!=-1){
            String currentRuppes =Ruppes[index]
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 2018-04-25
    • 2016-08-21
    • 2014-05-19
    • 2021-04-28
    相关资源
    最近更新 更多