编写一个学生类,包含属性有学号姓名年龄,将所有学生存入一个数组,自拟数据,用数组的初始化方法给数组
- 提问者网友:無理詩人
- 2021-03-12 08:55
- 五星知识达人网友:往事隔山水
- 2021-03-12 09:42
import java.util.ArrayList;
import java.util.List;
public class Student {
public static void main(String args[]){
StudentDto studentDto = null;
List
//students information
System.out.println("初始学生信息:
"); for (int i = 0; i < 5; i++){
studentDto = new StudentDto();
studentDto.setNumber(i);
studentDto.setName("student" + i);
studentDto.setAge(18 + i);
System.out.println("学号: " + studentDto.getNumber() + "; 姓名: " + studentDto.getName() + "; 年龄: " + studentDto.getAge() + "
");
students.add(studentDto);
}
//age + 1
for (StudentDto student : students) {
student.setAge(student.getAge() + 1);
}
System.out.println("修改后学生信息:
"); for (int i = 0; i < students.size(); i++){
System.out.println("学号: " + students.get(i).getNumber() + "; 姓名: " + students.get(i).getName() + "; 年龄: " + students.get(i).getAge() + "
"); }
System.out.println("年龄大于20的学生:
"); for (int i = 0; i < students.size(); i++){
if(students.get(i).getAge() > 20){
System.out.println(students.get(i).getName() + "
"); }
}
}
}
class StudentDto{
private int number;
private String name;
private int age;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
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;
}
}