本文主要是介绍请定义方法找出集合中所有姓张的人并且年龄大于18岁的并全部返回,然后在main方法中输出返回的人; 集合原始数据有: 张三,22 李四,26 张翠山,38 赵六,19 张三丰,1,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
public static void main(String[] args) {
Student stu = new Student();
Student s1 = new Student(“张三”,22);
Student s2 = new Student(“李四”,26);
Student s3 = new Student(“张翠山”,38);
Student s4 = new Student(“赵六”,19);
Student s5 = new Student(“张三丰”,103);
Student s6 = new Student(“张无忌”,17);
Student s7 = new Student(“赵敏”,16);
ArrayList list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
list.add(s6);
list.add(s7);
searchZhang(list);
}
private static void searchZhang(ArrayList<Student> list) {Iterator<Student> it = list.iterator();while (it.hasNext()) {Student s = it.next();if (s.getName().contains("张") && s.getAge() > 18) {System.out.println(s);}}
public class Student {
private String name;
private int age;public Student() {
}public Student(String name, int age) {this.name = name;this.age = age;
}public String getName() {return name;
}public void setName(String name) {this.name = name;
}public int getAge() {return age;
}public void setAge(int age) throws AttachOperationFailedException {if(age<18){throw new AttachOperationFailedException("年龄超出了范围");}this.age = age;
}@Override
public String toString() {return "collectDemo1{" +"name='" + name + '\'' +", age=" + age +'}';
}
这篇关于请定义方法找出集合中所有姓张的人并且年龄大于18岁的并全部返回,然后在main方法中输出返回的人; 集合原始数据有: 张三,22 李四,26 张翠山,38 赵六,19 张三丰,1的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!