定义一个静态方法,实现在Student类型的数组中,查找指定区间内是否有指定的学生姓名,如果有,返回true,如果没有,返回false

文章介绍了如何在Java中使用StudentUtils类的findStudentName方法,在给定的学生数组(students1)中查找指定区间的姓名,如例子中查找0到6索引位置的张三。
摘要由CSDN通过智能技术生成
public class StudentUtils {
//定义方法
    
    public static boolean findStudentName(Student[] students,int startIndex,int endIndex,String name){
        //遍历students数组的指定区间,从这个区间中查找是否有姓名为name的学生
        for(int i =startIndex;i<=endIndex;i++){
            //比较范围内的Student对象的姓名和你要查找的学生姓名
            if(name.equals(students[i].name)){
                return true;
            }
        }
        return false;
    }
}
//应用
public static void main(String[] args) {
    //创建多个Student类对象
    Student student1 = new Student();//调用Student类
    student1.name = "张三";
    student1.age = 20;
    student1.gender = '男';

    Student student2 = new Student();
    student2.name = "李四";
    student2.age = 22;
    student2.gender = '男';

    Student student3 = new Student();
    student3.name = "王五";
    student3.age = 18;
    student3.gender = '女';

    Student student4 = new Student();
    student4.name = "赵六";
    student4.age = 16;
    student4.gender = '女';

    Student student5 = new Student();
    student5.name = "孙七";
    student5.age = 23;
    student5.gender = '男';

    Student student6 = new Student();
    student6.name = "吴八";
    student6.age = 28;
    student6.gender = '男';

    Student student7 = new Student();
    student7.name = "周九";
    student7.age = 19;
    student7.gender = '男';

    Student[] students1 ={student1,student2,student3,student4,student5,student6,student7};

    //查找students1数组中0~6范围内是否有张三这个学生
    boolean result1 =StudentUtils.findStudentName(students1,0,6,"张三");
    System.out.println("你要查找的数组的指定区间内有你要查找的学生姓名:"+result1);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值