- public class SearchChar {
- public static void main(String[] args) {
- char[] ch = {'a','s','d','f','g'};
- char c = 's';
- int index = searchIndex(ch,c);
- System.out.println(index);
- }
- public static int searchIndex(char[] ch,char c){
- int index = 0;
- for (int i=0;i<ch.length;i++) {
- if(ch[i] == c)
- index = i;
- }
- return index;
- }
- }
复制代码
在方法中定义一个变量,用来记录找到char的角标,然后在把变量作为返回值就可以了 |