1 public class Homework02 {
2
3 //编写一个main方法
4 public static void main(String[] args) {
5
6 String[] strs = {"jack", "tom", "mary","milan"};
7 A02 a02 = new A02();
8 int index = a02.find("milan", strs);
9 System.out.println("查找的index=" + index);
10 }
11 }
12
13 //编写类A02,定义方法find,实现查找某字符串是否在字符串数组中,
14 //并返回索引,如果找不到,返回-1
15 //分析
16 //1. 类名 A02
17 //2. 方法名 find
18 //3. 返回值 int
19 //4. 形参 (String , String[])
20 //
21 //自己补充代码健壮性
22 class A02 {
23
24 public int find(String findStr, String[] strs) {
25 //直接遍历字符串数组,如果找到,则返回索引
26 for(int i = 0; i < strs.length; i++) {
27 if(findStr.equals(strs[i])) {
28 return i;
29 }
30 }
31 //如果没有,就返回-1
32 return -1;
33 }
34 }