如何在String数组中获取char的索引位置

时间:2015-03-02 11:11:10

标签: java arrays string char

如何在数组c中获取char chars的索引位置?。

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

// the answer is 4.

2 个答案:

答案 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);