[英]How to get the index of second or nth occurencece of a char without array only string class in JAVA
[英]How to get an index position of a char in the String array
如何获得char c
在数组chars
的索引位置?
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.