用java代码现在有5个学生,请把5个学生信息通过List方式进行存储、遍历和输出。 样例输入: 样例输出: 00005---小白龙---22 00004---沙悟净---26 00003---唐僧---28 00002---猪八戒---27 00001---孙悟空---25
时间: 2023-08-02 19:07:46 浏览: 116
java 接口应用 输出学生信息 代码实例
5星 · 资源好评率100%
下面是Java代码实现:
```java
import java.util.ArrayList;
import java.util.List;
public class Student {
private String id;
private String name;
private int age;
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return id + "---" + name + "---" + age;
}
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("00001", "孙悟空", 25));
students.add(new Student("00002", "猪八戒", 27));
students.add(new Student("00003", "唐僧", 28));
students.add(new Student("00004", "沙悟净", 26));
students.add(new Student("00005", "小白龙", 22));
for (Student student : students) {
System.out.println(student.toString());
}
}
}
```
输出结果为:
```
00001---孙悟空---25
00002---猪八戒---27
00003---唐僧---28
00004---沙悟净---26
00005---小白龙---22
```
阅读全文