在这里插入代码片
```import java.util.ArrayList;
import java.util.Iterator;
/*
二、定义一个学生类Student,包含三个属性姓名、年龄、性别,创建三个学生对象存入ArrayList集合中。
A:使用迭代器遍历集合。
B:求出年龄最大的学生,然后将该对象的姓名变为:小猪佩奇。
*/
public class z22 {
public static void main(String[] args) {
ArrayList<z2> ss = new ArrayList<>();
ss.add(new z2("1", 12, "女"));
ss.add(new z2("asf", 1, "男"));
ss.add(new z2("adsa", 3, "女"));
Iterator<z2> it = ss.iterator();
while (it.hasNext()) {
z2 s = it.next();
System.out.println(s.getName() + " " + s.getAge() + " " + s.getSex());
}
int a=ss.get(0).getAge();
for (int i = 1; i < ss.size(); i++) {
z2 p=ss.get(i);
if (p.getAge()>a){
a=p.getAge();
}
}
System.out.println(a);
for (z2 w : ss) {
if (w.getAge()==a){
w.setName("小猪佩奇");
}
System.out.println(w.getName()+w.getAge()+w.getSex());
}
}
}
java基础题
最新推荐文章于 2024-11-05 22:25:32 发布