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

2024-11-27 12:34:52
推荐回答(3个)
回答1:

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 students = new ArrayList();
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 students, Student s) {
students.add(s);
}

public static void deleteStudent(List 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);
}
}

}

}

回答2:

//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 stuList ,int id){

Student student = new Student();

if(stuList!=null){

Iterator 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 stuList = new ArrayList(); 

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

}


}

回答3:

好好学学java吧,这是饭碗啊。geek99. com