集合和泛型问题总结
1.定义一个学生类Student,包含三个属性姓名、年龄、性别,创建三个学生对象存入ArrayList集合中。A:遍历集合遍历输出。B:求出年龄最大的学生,然后将该对象的姓名变为:小猪佩奇。
分析:集合遍历 ,if判断
代码如下:
public static void main(String[] args) {
Student s1 = new Student("小明",10,"男");
Student s2 = new Student("小红",15,"女");
Student s3 = new Student("小刚",12,"男");
ArrayList<Student> list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
bl(list);
System.out.println("===========分割线=========");
int max = 0;
Student stu1 = null;
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
if(max < stu.getAge()){
max = stu.getAge()