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);
}