查找

import java.util.Scanner;
public class Eext {
    public static void main(String[] args) {
        String[] str = {"lala" , "koko" , "joke"};
        A02 a02 = new A02();
        int indes = a02.find("joke" , str);
        if (indes != -1) {
            System.out.println("找到了indes 下标为:" + indes);
        } else {
            System.out.println("未找到查询的字符串");
        }

    }
}

    //编写一个A02,定义方法find,实现查找某字符串数组中的元素查找,并返回索引,如果找不到返回-1
    //1.方法的返回类型 int
    //2.方法的名字 find
    //3.方法的形参 String strfind , String[] strs
    //4.方法体 查询
class A02 {
    public int find(String findStr , String[] strs) {
        for (int i = 0; i < strs.length; i++) {
            if (findStr.equals(strs[i])) {
                return i;
            }
        }
        return -1;
    }
}

 

posted @ 2022-12-12 21:40  书先生  阅读(101)  评论(0编辑  收藏  举报