java 作业:创建一个学生类,创建一个学生列表,将五个学生对象加入到列表当中,提供一个工具类,提供根据学号查询删除学生的功能。
明天晚上要交齐了,做完的赶紧交了!!!
已经有一个答案了,不过希望能集思广益,哪位大神帮忙做一下,真的感谢了,不说了,我现在就去补习JAVA,5555
//Student.java
public class Student {
private int id;
private String name;
private String grade;//班级
public Student(){}
public Student(int id, String name, String grade) {
super();
this.id = id;
this.name = name;
this.grade = grade;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
@Override
public String toString() {
return "学号 [id=" + id + ", 姓名=" + name + ", 班级=" + grade
+ "]";
}
}
//StudentUtil.java
import java.util.*;
public class StudentUtil {
public Student getStuById( List<Student> stuList ,int id){
Student student = new Student();
if(stuList!=null){
Iterator<Student> iterator = stuList.iterator();
while(iterator.hasNext()){
student = iterator.next();
if(student.getId()==id){
return student;
}
}
}
else{
System.out.println("没有学生!");
}
return student;
}
}
//Test.java
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Student> stuList = new ArrayList<Student>();
Student stu1 = new Student(201101,"王同学","2班");
Student stu2 = new Student(201105,"张同学","2班");
Student stu3 = new Student(201112,"张张同学","3班");
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu2);
System.out.println(new StudentUtil().getStuById(stuList,201112).toString());
}
}
本回答被网友采纳