如何在数组c
中获取char chars
的索引位置?。
public static final String[] chars = new String[]{"A","B","C","D","E","F"};
String c = "E";
// the answer is 4.
答案 0 :(得分:4)
如果您的数组已排序,则可以使用
Arrays.binarySearch()
int answer = Arrays.binarySearch(chars, c);
return answer;
如上所述。否则使用for循环。
for (int i=0; i<chars.length; ++i) {
if (s[i].equals(c)) {
return i;
}
}
答案 1 :(得分:3)
这是解决方案,希望这可以帮助你
String[] src = new String[]{"A","B","C","D","E","F"};
int biggerWher = Arrays.asList(src).indexOf("E");
System.out.println(biggerWher);