java 作业:创建一个学生类,创建一个学生列表,将五个学生对象加入到列表当中,提供一个工具类,提

java 作业:创建一个学生类,创建一个学生列表,将五个学生对象加入到列表当中,提供一个工具类,提供根据学号查询删除学生的功能。
明天晚上要交齐了,做完的赶紧交了!!!
已经有一个答案了,不过希望能集思广益,哪位大神帮忙做一下,真的感谢了,不说了,我现在就去补习JAVA,5555

package test;

import java.util.ArrayList;
import java.util.List;

/**
*
*
* @Author Ellrien 2016年4月14日 上午13:25:21
*/
public class Student {

private String name;// 学生姓名
private String no;// 学生学号
private String sex;// 性别

public Student(String name, String no, String sex) {
super();
this.name = name;
this.no = no;
this.sex = sex;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getNo() {
return no;
}

public void setNo(String no) {
this.no = no;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public static void main(String[] args) {
Student s1 = new Student("张三", "20160001", "男");
Student s2 = new Student("张四", "20160002", "男");
Student s3 = new Student("张五", "20160003", "男");
Student s4 = new Student("张六", "20160004", "女");
Student s5 = new Student("张七", "20160005", "女");
Student s6 = new Student("张八", "20160006", "女");

//添加学生到学生列表里
List<Student> students = new ArrayList<Student>();
StudentUtil.addStudent(students, s1);
StudentUtil.addStudent(students, s2);
StudentUtil.addStudent(students, s3);
StudentUtil.addStudent(students, s4);
StudentUtil.addStudent(students, s5);
StudentUtil.addStudent(students, s6);

//找到学号,删除掉这个学生。
StudentUtil.deleteStudent(students, "20160005");
}

static class StudentUtil {

public static void addStudent(List<Student> students, Student s) {
students.add(s);
}

public static void deleteStudent(List<Student> students, String no) {
Student student = null;
for (Student s : students) {
if (no != null && s.getNo().equals(no)) {
student = s;
break;
}
}
if (student != null) {
students.remove(student);
}
}

}

}
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2018-03-01

//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());

}


}

本回答被网友采纳
第2个回答  2016-04-14
好好学学java吧,这是饭碗啊。geek99. com
相似回答