我正在寻找一种方法来查找数组的索引位置,我想将该值存储在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"};
答案 0 :(得分:14)
你可以使用Arrays.asList
Arrays.asList(planets).indexOf("Mercury") // pass value
Arrays.asList(Rupps).indexOf("100") // pass value
或
Arrays.asList(planets).indexOf(2); //pass index
答案 1 :(得分:3)
private int foundIndex(String query) {
for (int i = 0; i < planets.length-1; i++) {
if (planets[i].equals(query)) {
return i;
}
}
return -1;
}
...代码:
int index = foundIndex("Venus");
if(index!=-1){
String currentRuppes =Ruppes[index]
}