如何获取字符串数组中字符的索引位置

How to get an index position of a char in the String array

如何获取数组 chars 中字符 c 的索引位置?

public static final String[] chars = new String[]{"A","B","C","D","E","F"};
String c = "E";

// the answer is 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;
   }
}

这是解决方案,希望对您有所帮助

    String[] src = new String[]{"A","B","C","D","E","F"};
    int biggerWher = Arrays.asList(src).indexOf("E");
    System.out.println(biggerWher);