如何獲得字符串c
中字符索引位置chars
?如何獲取字符串數組中字符的索引位置
public static final String[] chars = new String[]{"A","B","C","D","E","F"};
String c = "E";
// the answer is 4.
如何獲得字符串c
中字符索引位置chars
?如何獲取字符串數組中字符的索引位置
public static final String[] chars = new String[]{"A","B","C","D","E","F"};
String c = "E";
// the answer is 4.
這裏是解決方案,希望這可以幫助你
String[] src = new String[]{"A","B","C","D","E","F"};
int biggerWher = Arrays.asList(src).indexOf("E");
System.out.println(biggerWher);
如果你的數組進行排序,可以使用
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;
}
}
看起來像一個解決方案 – maxormo 2015-03-02 11:21:12
如果你的數組進行排序,你可以用'Arrays.binarySearch()'。 – 2015-03-02 11:12:54
如果不是,只需使用'Arrays.asList(chars).indexOf(c)' – 2015-03-02 11:15:20
@Florent Bayle:是的,它被排序。你能舉個例子嗎? – 2015-03-02 11:15:24